Evertjan. said:
Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javascript:
As I wrote in the parallel? posting:
The default object is just the object that is "assumed" if no definite
object is specified.
... when calling that object in the script.
So, in browser js:
window.myValue is the same as myValue,
unless in a subscope myValue is redefined
[= has a pointer to another object/property/method]
The expression "myValue" is a variable expression. It resolves the
variable wrt. the current scope. In Javascript it is a lexical scope.
If no variable with that name exists in the scope, then Javascript
considers the variable to be a global variable. Global variables
are properties of the global object.
And "window.myValue" is a property lookup on the global object, if
"window" resolves to a reference to the global object.
As I read your explanation, it seems that you consider "myValue" as
being equivalent to "window.myValue" because it is equivalent to a
property lookup, with the global object as a "default object" where
"myValue" has no explicit object of the property.
I consider variable lookup and property lookup to be very different
processes. Naming an object that holds the global variables the
"global object" is perfectly reasonable to me, and calling it a
"default object" is misleading, because "myValue" is not treated
as a property lookup on a default object.
It would be nice of each subscope could be given a named default subroot
object, because now only the myValue of the root scope's and the present
scope's myValue are callable,
they are the only ones that can be accessed, yes.
not the in-between-scope's myValue-s.
Indeed. A lexical scope means that you can only access the closest
enclosing declaration of a given name. However, being lexically scoped,
whoever wrote it would have access to all the code of the scope, and
could rename one of the variables if the shadowing wasn't intentional.
Yes, just like any other object, but that's why naming that "global" is not
right. It is the root object of the root scope.
Technically the global object is not in the scope chain. It is
accessed only when the scope chain fails to resolve a variable
name. Effectively, though, it is almost as if the global object was
at the root of all scope chains.
Theoretically you could set another object as the default object in the
root, so that the original root object is no longer the default.
[Perhaps not possible in present day JS, but that is only implementation.]
Even in present day Javascript, if you access a function defined in another
page, it will have a different global object.
Seen as a tree, you could call the object the "global root object",
but why shorten that to "global object"? Beter shorten that to "the root
object"
It's a matter of intent, I guess. The global object holds the global
variables. A root object would be at the root of the scope chain.
The authors of Javascript had the former perspective.
Even the naming pointer to this object can be overwritten to mean another
object, either in the root scope or any other scope. This could make the
global root object effectively unreachable in that scope.
The property "window" of the global object can probably be changed, or
shadowed by a local variable. However the global object is always accessible,
e.g.,
var global = (function(){return this;})();
Objects are only accessable in JS by their naming pointer. different scopes
can use the same name for different objects.
Objects are passed around as references. Those references can be
stored in variables or object properties ("naming pointer"), but they
can also be the return value of a function.
In the same sense there are no local objects, just local names pointing to
objects.
There are no local objects. The only thing that is local (to a
function call) are local variables. That includes arguments
(also accessible as properties of the arguments object).
If an object is defined in a local scope, however, it would at present be
difficult to export the use of such object to another scope that is not a
"subscope" of that scope, just because there is no way to move the pointer
to that other scope.
There are two ways to have values exit a function call: store it in a
place that is also accessible outside the call (global variable or
local variable in an enclosing scope) or return it as the result
of the function. The latter requires no variables/names.
No, the default object is the object that does not have to be named in that
scope.
That's one view of what happens when you access a global variable.
However, that view is flawed as the expression "myVariable" is only
relative to the global object if it is not a local variable in scope.
It's not a property lookup where the target object "isn't named".
Perhaps I used that where I ment to say the root object. In JS, it
usualy is the same object, but that is implementation.
It's specification. The ECMAScript specification states that the
global object is where unresolved variables are finally attempted
resolved against. I.e., it's what you call the root object of scope
based variable resolution.
It is also the variables object of global code (code not inside a
function body).
I just object to using the word global here for the root object,
becaus it gives the wrong idea,
And I think it is the right idea.
that it is the only object available in all [sub]scopes.
That's not what "global object" means. But it is true that it is
accessible in all scopes. If a function is called directly, and not as
a method (referenced as "object.property"), then "this" refers to the
global object in the method body.
All other objects of [= defined in] the root scope have the
same possibilities, as long as the name is not reused.
Yes, that's just basic scope rules.
/L