9

Every property of the window object is a global variable. This means you can do something like this:

window.foo = 42;
alert(foo); //alerts 42
var bar = 3;
alert(window["bar"]); //alerts 3

Because of this, I've always wondered what the purpose was of referencing window in code like this

if(window.prompt("Enter Password") === "secret") {
  window.location.hash = "authorized";
  window.open("secretPage.html");
}

when you could omit window and have code that does exactly the same thing:

if(prompt("Enter Password") === "secret") {
  location.hash = "authorized";
  open("secretPage.html");
}

The use also seems inconsistent; I almost never see window.alert but I'll frequently see window.location.

Do people just like referencing window to make their code more verbose? Is there some good reason for doing this that I don't know about?

3
  • window is just a property of the global object. Commented Jan 28, 2012 at 0:37
  • 2
    Which is interesting, because that means window === window.window.window.window.window.window Commented Jan 28, 2012 at 1:18
  • yes this is true. it doesn't matter if you use alert(), window.alert() or window. window. window.alert(). (eval() is an exeption) In the global scope you can also use this.alert() Commented Jan 28, 2012 at 1:24

3 Answers 3

6

One situation in which it helps to be explicit is that it will be immediately clear inside a function that you intend to modify a global variable. For example:

function blah() {
    // a bunch of code preceding...
    test = "aha!";
}

Did someone forget to declare test with var? Or is this intentional modification of a global? Compare:

function blah() {
    // a bunch of code preceding...
    window.test = "aha!";
}

Now it's immediately clear what is intended. Of course, you know, globals should be avoided in the first place, but you get my point.

Sign up to request clarification or add additional context in comments.

Comments

3

window is implicit, but it's good code practice to avoid ambiguity where possible.

5 Comments

It would be good to avoid ambiguity, but I'm not sure I see how it could be ambiguous.
suppose you wrote a function "open" that did something other than opening a new window? Or maybe an alien js file that you imported polluted your global namespace.. then you're better off using fully qualified references.
If you made a global variable var open = 3; and tried to do window.open() it would cause an error because window.open has been overwritten. Alien js files that pollute the global namespace will change the expected behavior regardless of whether you use open or window.open.
Peter Olson, I upvoted your comment after testing it out. Its correct that window is polluted in either case - even with a local variable as Gareth asked.
@Gareth: What if you have a local variable with the name window?
2

I think it's generally used to avoid potential clashes with other variables in an enclosing closure. Also, style checkers like jslint will raise errors if you access global properties without an explicit object (except for well-known properties like window itself.)

That said, in a browser, when you get into multiple windows, e.g., with iframes, etc., you may need to explicitly pass around a reference to the window object. (And if you really care about the details, the window object, or at least references to it, are very special and can't even be described in standard javascript.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.