VK wrote:
The provided resources contain a lot of useful info about
JavaScript closures and function/method functioning. I
suggest to read them and even bookmark them.
I suggest that as some point _you_ try reading and _understanding_ them,
as bookmaking then is hardly a useful substitute.
<snip>
.... More officially the default object is the one placed
at the top of the scope chain (where the bottom of this
chain is always points to the global scope). If the
requested property
"Requested property" is potentially confusing and misleading. The _only_
constructs in javascript that are resolved against the scope chain are
Identifiers, while 'requesting' a 'property' would be implied in property
accessor resolution.
is not presented in the local scope,
the engine goes further by the scope chain and it looks
at the global scope. If still no luck then the engine gives
up and it errors out.
Because instead of
window.alert('Hi!')
window.document.write('Hi!')
we can (and mostly do) use
alert('Hi!')
document.write('Hi!')
some language references state that [window] object *is* the
default object by default. That is an error.
Are you saying that some references state that the window object is the
first object on the scope chin by default (whatever "by default" is
supposed to mean in this context)? You can cite these "references"?
Of course if you insist on using "default object" as a label for the
object at the top of the scope chain then the window/global object is the
"default object" in the global execution context. And as the global
execution context is the execution context that code always first enters,
and so may be regarded as the "default" (entering any other execution
context requires explicit scripted instructions), it would be true to say
that window/global object is at the top of the scope chain' by default'.
[window] object has nothing to do
with the default object;
Apart from being the object at the top of the scope chain (your "default
object") in the ('default') global exect5uion context.
but because [window] and Global object are
made to act as one unit,
There is no practical sense in which they are not the same object.
the engine will find window fields and
methods at the end of the scope chain, just like it
finds variable
Which is what would be expected if they are the same object.
foo in var foo = 'bar';
function f() {
alert(foo);
}
The resolution of 'alert' and 'foo' identifiers above
goes by equivalent schema: with searching 'alert' and
'foo' in the local scope, failing - then finding them
in the global scope.
The whole mechanism of closures relies on the fact that there may be more
than two objects on the scope chain. What you are referring to as "the
local scope" is the Activation/Variable at the top of the scope chain
(the thing that would be your "default object" by default). there may be
numerous other Activation/Variable objects on the scope chain (along with
any objects added deliberately added to the scope chain of functions
using the - with - statements).
The indefinite length of the scope chain, and the fact that Identifier
resolution works down the chain kink by link, is one of the reasons that
it is referred to as a 'chain'. If it was as simple as just a local
object and a global object then the two could be described as 'the local
scope' and the 'global scope', but the real mechanism is more complex
than that.
That is why in time-crucial applications (say graphics
intensive ones) it is a good idea to use fully-qualified
path to window methods:
You are about to put your foot in your mouth agian.
it saves the engine from searching and failing first in
the local scope. Depending on the complexity of the local
scope structure the productivity gain for looped calls can
be very noticeable.
Pure bullshit! What you are referring to as a "fully-qualified path to
window methods" is using the property accessor - window.alert - in place
of the Identifier - alert -, for example. What you are failing to take
into account is that the - window - in - window.allert - is an
Identifier, and so will be resolved against the scope chain.
To resolve the Identifier - alert - the engine will examine the object at
the top of the scope chain to see if it (or an object on its prototype
chain, if any) has a property named "alert". If we assume that that
object has no "alert" property then the engine moves on to the next
object on the scope chain and does the same test. The global object is
the last object on any scope chain, so if we assume that no object higher
up the chain had an "alert" property than the resolution of the
Identifier ends when the "alert" property of the global object is found.
If the property accessor - window.alert - is resolved the fist step will
be resolving the Identifier - window -, which uses the same mechanism as
the resolution of the Identifier - alert -. If we assume that no other
object on the scope chain has a property named "window" then the process
ends when the "window" property of the global object is found. So the
resolution of the Identifier - window - will take almost exactly the same
amount to time as the resolution of the Identifier - alert - (if we are
using the same scope chain as with - alert -). However, resolving the
property accessor - window.alert - is not over yet, as now the object
referred to by value of the "window" property of the global object needs
to be examined to see if it has a property named "alert".
The logic of the mechanism says that property accessors referring to
properties of the global object will take longer to resolve than
Identifiers referring to properties of the global object. And you may
recall that the list time you were corrected on this point you were shown
code that demonstrated an empirical test where the results showed
Identifier resolution to be faster property accessor resolution, even on
Windows IE, where there is most reason to expect the outcome to the
other.
Th notion that "fully-qualified paths" are quicker is an old wives tail,
that if it was ever true certainly is false with current browsers.
In some languages current object ...
<snip - waffle>
.... only one usable alternative.
So may words, so little understanding.
Richard.