tags:

views:

88

answers:

3

Why does the DOM have an object called self and another called window when they are the same thing? To add to the confusion window has a property called self so:

window === window.self === self

Why is it like this? Which one should I use?

A: 

When you're calling self it is window.self, so same thing there like any other global property (e.g. location is really window.location).

The reason it's there? Usually for checks like this:

if(window.top != window.self) {
  alert("We're in a frame");
}
Nick Craver
But what makes that better than `if (window !== window.top)`? (And you'd want the strict operator, wouldn't you?) E.g., what's `self` for, as it `===` `window`?
T.J. Crowder
@T.J. If `window.top` and `window.self` have changed types you have bigger issues in your JS :) As for the first check, yes you could use `window`, but `window.self != window.top` is a lot *clearer* was my point :)
Nick Craver
@Nick: Gotcha. (And on type: Yeah, but hey, it could happen! `window.top = "frog";` ;-) )
T.J. Crowder
@T.J. Oh it can definitely happen :) Older IE letting you redefine the `window` object itself, good times
Nick Craver
A: 

The global self is a shortcut to window.self. They are both the very same object.

As to why there are both window and self, in a web page context, self is always equal to window (as far as I know). But I guess this doesn't have to always be that way - Javascript/ECMAScript is not limited to use in a web page where there is a DOM.

As to what to use, both should be safe to use in a normal web page context AFAIK. See @Nick's comment for good arguments for using window, though.

Pekka
I wouldn't use `self` personally...many frameworks/misc code use this as a local variable internally and possibly in the closure you're feeding it, so `self` may *not* be `window.self` in some cases.
Nick Craver
@Nick good points, edited my answer to reflect them.
Pekka
+1  A: 

self is defined by the javascript environment and points to the [global] object (but is not part of the spec, so might not be there), while window is part of the DOM specification. In most browsers the window is used as the [global] object, but this is not always so.

That self == window.self is not strange as they are the same object - when self is being looked up, it is found as a property of the global object (window). So it is in fact the same as window.self == window.self.

As noted elsewhere, to reliable reference the [global] object, you should define it your sef by running var global = this; in the global execution context.

Sean Kinsey
I understand that both are pointers to the same object. It just caught me as weird that an object of the DOM has as a property itself because it is not evident why anyone would intentionally design something to be like that.
hekevintran
Its not weird at all if you know of the execution contexts are built up in ECMAScript. Afterall, the `self` property has to be defined somewhere accessible right? And that is as a property of the `[global]` object itself. That the property actually points to the `[global]` object is not really relevant here.
Sean Kinsey
So are you saying that `self` is like `var global = this;` in the global execution context? I guess my question is: why does `self` exist if the browser gives us `window`?
hekevintran
Wait is it because `self` used here is a global variable and all global variables are properties of the global JavaScript object which is also called `window` by browsers?
hekevintran
Because in the ECMA spec there is not direct reference to the `[global]` object, so some implementers extended the implementation with `self`. Now remember that DOM is not really bound to javascript (equally accessible with vbscript in IE) so `window` is not really part of the equation when defining the global object. BUT as stated the DOM's window object is normally used as the `[global]` object.
Sean Kinsey
@hekevitran That is pretty much it, although the [global] object isn't 'called' window, its the DOMs window that is _used_ as the global object. Its not really easy to explain using comments only :)
Sean Kinsey
@hekevintran: *"Wait is it because `self` used here is a global variable and all global variables are properties of the global JavaScript object which is also called `window` by browsers?"* Yes. In fact, the global varible `window` is, itself, a property of the global object, just like `self`, just like my `global` example. This is because the global object (`window`, on browsers) is the "binding object" of the global execution context. http://www.ecma-international.org/publications/standards/Ecma-262.htm
T.J. Crowder
Right, I get it now. That was a lot simpler than I thought it was :)
hekevintran
Might be worth mentioning that in ECMAScript 5th's strict mode (`"use strict";`), `var global = this;` would fail, because trying to access the global object is a violation in strict mode. Try it for yourself at JSLint.com.
Andy E