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?
selfis defined by the javascript environment and points to the [global] object (but is not part of the spec, so might not be there), whilewindowis part of the DOM specification.In most browsers the
windowis used as the [global] object, but this is not always so.That
self == window.selfis not strange as they are the same object – whenselfis being looked up, it is found as a property of the global object (window). So it is in fact the same aswindow.self == window.self.As noted elsewhere, to reliable reference the
[global]object, you should define it your sef by runningvar global = this;in the global execution context.