Klaus said:
Is the following code valid and supported by current implementations?
Yes, but the chances that you actually want to do this are close to
zero.
The execution of global code commences with "variable instantiation"
using the global object as the Activation/Variable object in the global
execution context.
First function declarations are evaluated and named properties created
on the Variable object with names corresponding to the function names
used and then references to the function objects created using the
function definitions are assigned to those named properties.
function somename() {
this.show = function () { document.write("somename called") }
}
So at this stage the global object has a property named "somename" that
refers to a function object.
The next stage in variable instantiation is to create named properties
of the Variable object for each declared (with the - var - keyword)
local variable (local variables in global code become global variables).
However, if the Variable object already has a named property
corresponding with the identifier used in a variable declaration no new
property is created and its original value is unaltered. So - var
somename - does not result in the creation of a new property of the
Variable object, and the existing "somename" property of that object
continues to refer to the declared function.
That is all of the variable instantiation required by this code so
execution of the global code will follow.
The first statment:-
var somename = new somename();
- resolves the right had side first, the identifier - somename - is
subject to resolutin against the scope chain, which includes the
Variable object for the global execution context so - somename - is
resolved as a property of that object, the property referring to the
function object created with the function declaration.
The - new - operator calls that funciton as a constructor and a
reference to an object is returned. So the right hand side of the
assignment expression evaluates as a reference to an object.
Next the assignment is performed. To know where to assign the value the
left hand side the identifier - somename - is scope chain resolved,
again as a reference to a named property of the Variable object. And the
reference to an object that was the evaluated result of the right hand
side expression is assigned to that named property of the Variable
object. Replacing the reference to the function object that had
previously been the value of that property.
As a result any further attempts to use the identifier - somename - in a
function or constructor context will result in an error as it now refers
to an object.
somename.show()
Note that the class name "somename" is reused for the variable name.
But only one named property of the Variable object for the global
execution context is created, and that property can only hold one value
at a time.
Richard.