Home Ask Login Register

Developers Planet

Your answer is one click away!

Pedro Otero February 2016

Am I creating a global variable?

I'm creating a function that tests Angular filters using Jasmine. The specs are ran by Karma. The idea is to pass a module and filter name, with an array of tests to be performed on it. Every element of that array has a description, an input value and an expected result. But it also could contain a testSteps function for tests that are not as simple as passing an input value and expecting an exact value. The function I'm making is meant to create the corresponding Jasmine describe and it blocks for the tests, and it's inside a global variable defined like this:

var $uite = {
    testFilter : function(filterName, moduleName, tests) {
        this.filterObject = null;
        this.isDefined = function(vble) {
            return typeof (vble) != 'undefined';
        describe('FILTER: ' + filterName, function() {

            beforeEach(inject(function($filter) {
                filterObject = $filter(filterName);

            for (var i = 0; i < tests.length; i++) {
                var test = tests[i];
                it(test.it, function() {
                    if (typeof (test.forInputValue) != 'undefined') {
                        var result = filterObject(test.forInputValue);
                    } else if (typeof (test.testSteps) != 'undefined') {

The code for my tests is:

$uite.testFilter('nameFilter', 'app', [ {
    it : 'gets male gender from a name ending in O',
    forInputValue : 'Pedro',
    resultShouldBe : 'el tal Pedro'
}, {
    it : 'gets female gender from a name ending in A',
    forInputValue : 'Carolina',
    resultShouldBe : 'la tal Carolina'
}, {
    it : 'transforms Lina into la tal Lina',
    testSteps : function() {
        var result         


Josh Beam February 2016

Your question: Am I creating a global variable?



Full answer

It is global. You're making it global here:

filterObject = $filter(filterName);

If you don't put var before that, and you're not using strict mode (e.g. use strict;), then it will be a global.

If you want it to refer to this, you need to use this everywhere:

beforeEach(inject.call(this, function($filter) {
    this.filterObject = $filter(filterName);

Or something like that. Alternatively, you can declare var _this = this:

beforeEach(inject(function($filter) {
    _this.filterObject = $filter(filterName);

Everywhere else throughout your code, you'll need to refer to it as this.filterObject.

FYI, a note about strict mode (article on MDN) (I always suggest using it, because it catches accidental globals) from the section "Converting mistakes into errors":

First, strict mode makes it impossible to accidentally create global variables.

Additional references:

  1. Function.prototype.bind on MDN
  2. Function.prototype.call on MDN

P.S. Looks like you're coming from a Java background. When using objects, defining this.<whatever> and then trying to refer to it as simply <whatever> won't work in JavaScript like you'd expect it to work in Java.


To access filterOb

Post Status

Asked in February 2016
Viewed 3,006 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved