Fox said:
Eli wrote:
window.open() is more correct...
There is no measure of relative "correctness" between the two. They are
both valid javascript syntax (examples of the CallExpression
production).
open() will attempt to call that method on the "current
object".
If any object is to be considered the "current object" it would be the
object referred to bye the - this - keyword. The resolution of an
unqualified identifier is against the scope chain and the object
referred to by - this - will not necessarily be on the scope chain
(although the global object is always on the end of the scope chain and
is also most often also the object referred to by - this -).
Given the code:-
open();
- the objects in the scope chain are examined in turn to see if they (or
one of their prototypes, if any) have a property called "open". If one
of them does then a value of the internal Reference type is returned
with the object in question assigned as its "base" object and "open"
assigned to its property name. Then the internal GetValue fucntion is
called with that Reference type as its argument and if the returned
value is an Object that implements the [[Call]] method (so a function)
then it is executed.
Because the global object is always at the end of the scope chain and
the global object is the window object, if not other object on the chain
has an "open" property then the function called is the "open" method of
the window object.
When the code:-
window.open();
- is used the initial identifier resolution against the scope chain is
for "window". in web browser the global object has a property called
"window", that is itself a reference to the global object. So the
identifier resolution returns a reference to the global object. Then the
"open" property name is looked up on that object to acquire a reference
to the 2open" method, prior to its execution.
As a result both expressions execute the same function, but the second
is fractionally slower as it involves an additional step.
I suspect IE is much more lenient about this syntactical
mistake.
There is no syntactical mistake in either expression.
There is also a document.open() as well that is very
different ( ... .)
And when a document is on the scope chain that would be significant as
it would probably be above the global object and examined before it.
Some browsers, or scripting environments
might complain if you are not more specific.
When some browsers generates functions form source code provided as text
string values for event-handling attributes in the HTML source they
provide those functions with custom scope chains, and sometimes they do
place the document on those scope chains. Because browsers may or may
not provide these custom scope chains, and when they do they may or may
not place the document on that scope chain, it is safer to use -
window.open() - in code provided as an event handling attribute in the
HTML source. In normal javascript contexts the scope chain is
sufficiently clearly specified to allow the author to understand any
consequences of using - open() - instead of - window.open() - as
without action on the part of the author there would be no issue with
using - open() - (beyond the usual consideration of whether the browser
supports an open method at all), and it would be more efficient.
Richard.