Newsgroups: comp.lang.javascript
From: Lasse Reichstein Nielsen <l...@hotpop.com>
Date: Sun, 24 Aug 2008 13:55:34 +0200
Local: Sun, Aug 24 2008 7:55 am
Subject: Re: Why "window.alert()" over "alert()"?
Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
> Gregor Kofler wrote: It does seem that the global object's "window" property holds a >> In case of "alert()" JS will search through the scope chain, until an >> alert-named function is found. It will finally end up at the global > ^^^^^^^^ property ^^^^^^^^^^ >> object, in browsers the window object. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > This assertion is based on wishful thinking, still not backed up by any > public standard or exhaustive research. You would be well-advised not to > advocate it. reference to the global object itself in all current browsers, to the point where a new browser would be causing itself problems by not doing the same. No standard, true, except de-facto standard. > In fact, there is evidence to the contrary: MSHTML's creating "read-only" They are equally available through the global object. They are not > properties of a host object in the scope chain to refer to element objects > representing elements with ID or name, also being available through the > `window' reference. ReadOnly as properties of the global/window object, though, only when resolved through the scope chain. > But the Global Object is specified as a native object, That, or IE doesn't follow the specification and implements [[Put]] > it has to implement the [[Put]] method exactly as it was specified; so that > cannot be the object referred to by `window', which exhibits said behavior > instead. differently on the global object. It does appear that MSHTML has an a step in the variable resolution Example: <div id="foo">foo</div> alert("foo" in window); try { foo = 42; } catch(e) { alert(e.message); } window.foo = "window.foo"; </script> Using the global object instead of "window" gives exactly the same I.e., the scope-lookup appears to match page id's just before it The property is not writable, but it does shadow the same property It appears that the id-elements are *also* properties of the And further, if a variable or function with the same name is declared Even more interesting, the behavior depends on whether one reads the <script type="text/javascript"> In the above, if [2] is uncommented, the final alert changes If [1] is also uncommented, the final alert changes back again. Ok, this was a diversion. The point is: There global object and My bet is that you can expect that to stay true. ... > That is not the reason why. Because if someone declared `window' in the That's not being "absolutely sure". The "global" property could > scope chain before the Global Object (so as this would not refer to the > `window' property of the Global Object anymore), then the issue remained. > It is instead that it is unlikely that someone would do that. > To be absolutely sure, one would have to feature-test and use equally well have been overwritten or shadowed, and it's probably even more likely than someone shadowing "window" or "alert", since those are well known global properties. To be absolutely sure, you should create a reference to the global e.g. (which, by the way, alerts "true" in all the browsers I have available, /L You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
