Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Take the following code...

window.thisPage = {
    MyObject: function() {
        //private var
        var x = 1;

        //private method
        var inc = function() {
            x = x+1;
        };

        //public method
        this.output = function() {
            return x;
        };

        inc();  //so x will always initially start out at 2
    }
};

obj1 = new window.thisPage.MyObject();
obj2 = new window.thisPage.MyObject();
obj3 = new window.thisPage.MyObject();
obj4 = new window.thisPage.MyObject();

alert(obj2.output());

I understand now why window. is proper when defining "thisPage" because that is explicit (based on this answer). But does that mean that I should continue to type window. like in the area where I am creating the 4 objects? I know it will run either way. Just wondering what others think is proper and why. Or does it really not matter?

share|improve this question
1  
In fact, you never should have assigned to window, just use a global var declaration. –  Bergi Oct 2 '14 at 19:35
1  
However, what lacks a var declaration are your obj1obj4 variables –  Bergi Oct 2 '14 at 19:35

2 Answers 2

Do you have to use global variables?

Well, lets imagine that you have a big application with big amount of models, view and controllers (you are using MVC) or you just want to splite logic of one big file.

What there going to be, if you will use var? All our global names will be encapsulated in js file separetly and you will look for some way of comunication between components of your application.

Easiest way is to define one variable in global scope and use it as entry point.

How to define global variable?

There are not difference between following lines

window.hello = function () { alert('Hello!'); }
hello = function () { alert('Hello!'); }

If you didn't know, document, alert, prompt and so are all members of window object, in other words window shares its properties as global scope.

Notes

Use following approach for your modules

(function(window, undefined) {
    // you code goes here
})(window);

In this case if you will want to join all your modules in single file (to increase page loading time by decreasing number of needed files) you may be disapointed with bugs that can be follow this action.

Current approach will provide bugs free solution, as all of you local variables existing only inside wrapper function.

share|improve this answer

To directly answer your question: No, you don't need to write window. when accessing previously defined global variables... unless you have a local variable or closure that's shadowing the global one, and you have to do some scope resolution.

That's just a fancy way of saying that you can have a local variable with the same name as the global one. So if you want to ensure that you're accessing the global one, you'll have to use window.. Otherwise, JS will access the variable that's "closest to you," i.e. the local one. That's what's meant by "shadowing": The local variable casts its shadow on the global one.

For instance, consider this code:

// global variables
var x = "the global x";
var y = "the global y";
var z = "the global z";

// define a function
function funcA() {
  // local variables in funcA's scope (note that we don't shadow z)
  var x = "funcA's x";
  var y = "funcA's y";

  // define a nested function
  function funcB() {
    // local variables in funcA's scope (note that we don't shadow y or z)
    var x = "funcB's x";

    write("Inside funcB, x is " + x);
    write("Inside funcB, y is " + y);
    write("Inside funcB, z is " + z);
  }

  write("Inside funcA, x is " + x);
  write("Inside funcA, y is " + y);
  write("Inside funcA, z is " + z);
  write("---");
  funcB();
}

write("In the global scope, x is " + x);
write("In the global scope, y is " + y);
write("In the global scope, z is " + z);
write("---");
funcA();

// Helper function
function write(string) {
  document.getElementById("output").innerHTML += string + "\n";
}
<pre id="output"></pre>

Run the snippet, and should see how shadowing works out.

Yet, if you replace all those references to x, y and z with window.x, window.y and window.z, you'll always be accessing the global ones... unless you've accidentally shadowed window with a local variable. In that case... well, things get complicated. So just don't do that :)

Be sure to note Bergi's comments; window is the global object for JavaScript running in a browser, but in other runtimes (e.g. Node.js) there's nothing called window. In my previous answer (the one you linked to), I forgot to mention that distinction (though I've since updated the answer).
Long story short: If you're targeting browsers only, go ahead and use window. You'll probably also rely on other stuff only browsers implement. But if your code isn't actually reliant on a browser (like you counter code), you can just write var ... in the global scope to create a global variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.