12

Are there any benefits to using the 'window' prefix when calling javascript variables or methods in the window object? For example, would calling 'window.alert' have an advantage over simply calling 'alert'? I can imagine using the prefix could give a small performance boost when the call is made from inside some function/object, however I rarely see this in people's code. Henceforth this question.

1
  • alert is a bad example, because it's typically only called once and it doesn't matter (generally) how long it takes to display. Commented Jul 17, 2009 at 17:05

7 Answers 7

13

This is useful when attempting to test global object values. For example, if GlobalObject is not defined then this throws an error:

if(GlobalObject) { // <- error on this line if not defined
    var obj = new GlobalObject();
}

but this does not throw an error:

if(window.GlobalObject) { // Yay! No error!
    var obj = new GlobalObject();
}

Similarly with:

if(globalValue == 'something') // <- error on this line if not defined
if(window.globalValue == 'something') // Hurrah!

and:

if(globalObj instanceof SomeObject) // <- error on this line if not defined
if(window.globalObj instanceof SomeObject) // Yippee! window.prop FTW!

I would not expect to see a significant performance difference, and the only other reason you might do this is to ensure that you are actually getting a value from the global scope (in case the value has been redefined in the current scope).

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

Comments

9

I doubt there is any measurable performance benefit. After all the scope chain would be scanned for the identifier window first then the window object would be scanned for the desired item. Hence more likely it would be deterimental to performance.

Using window prefix is useful if you have another variable in scope that would hide the item you may want to retrieve from the window. The question is can you always know when this might be? The answer is no. So should you always prefix with window? What would you code look like if you did that. Ugly. Hence don't do it unless you know you need to.

2 Comments

I had no idea the scope chain would be scanned for window! I always thought window was a reserved keyword, and it thus wouldn't happen. Thanks.
The scope chain actually works the other way around. Local variables override globals.
7

Retrieved from Google (http://www.techotopia.com/index.php/JavaScript_Window_Object):

The window object is the top-level object of the object hierarchy. As such, whenever an object method or property is referenced in a script without the object name and dot prefix it is assumed by JavaScript to be a member of the window object. This means, for example, that when calling the window alert() method to display an alert dialog the window. prefix is not mandatory. Therefore the following method calls achieve the same thing:

window.alert()
alert()

However, I read but have not had time to test the following from: (http://www.javascriptref.com/reference/object.cfm?key=20)

One place you'll need to be careful, though, is in event handlers. Because event handlers are bound to the Document, a Document property with the same name as a Window property (for example, open) will mask out the Window property. For this reason, you should always use the full "window." syntax when addressing Window properties in event handlers.

Comments

1

As far as performance, I think AnthonyWJones has it covered.

One use of the window prefix is to explicitly make something available outside the current scope. If you were writing code in a self-invoking function to avoid polluting the global scope, but there was something within that you did want to make globally available, you might do something like the following:

(function(){
    function foo(){
        //not globally available
    }
    function bar(){
        //not globally available        
    }
    window.baz = function(){
        //is globally available
        foo();
        bar();
    };
})();

1 Comment

baz = function() { /* ... */ } does the same, and no, AnthonyWJones doesn't have it covered. The standard guarantees there to be a measurable performance benefit (not just in JS, but also in languages like Lua), due to how scopes work.
0

I imagine that the performance benefit here is amazingly insignificant at best, if there is one at all.

Comments

0

It only matters if you're using frames and doing a bunch of javascript calls across frames, and even then only specific scenarios warrant the necessity of referencing window explicitly.

Comments

0

When you use the prefix, you're making it explicit you're using the "global" definition of the variable, not a local one. (I'm not sure whether / how you can inject variables into a scope in JS, except the weirdness with this and inline event handlers.) YMMV, you may either prefer the clarity, or find it to be just clutter.

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.