Jorge meinte:
Not really.
To quote the relevant part of the spec:
| 10.1.3 Variable Instantiation
|
| Every execution context has associated with it a variable
object.
| Variables and functions declared in the source text are added as
| properties of the variable object. For function code, parameters
are
| added as properties of the variable object.
http://bclary.com/2004/11/07/#a-10.1.3
As stated: function() { } declarations are evaluated *before* anything
else. Your function g() gets then overwritten by var g.
Stepwise, g is bound to the Variable object with value undefined.
Then Function g replaces the value that already existed (undefined)
with a function object.
Next pass. g is assinged to the value +"Hola"
an alert call with string value argument.
javascript
function(){var g= "Hola";var g= function () { ; };alert(g
+", "+typeof g);})();
var g - creates a property on the variable object with name -g - and
value undefined.
var g - ignored.
second pass.
g is assigned the value "Hola".
g is assingned the value of a functionExpression.
an alert statement with string value argument
In this case your var g (the second var can be omitted) just gets a new
value assigned.
Actually, either - var - statment could be omitted and the interpreted
code would have the same result.
(function(){
g = "Hola";
var g = function () { ; };
alert(g +", "+typeof g);
})();
FunctionDeclarations and Variable declarations are evaluated in the
first pass. They both create a property to the Variable object if no
such property exists.
_Source code_______ _value________________________________________
parameter variable The value passed in the call (undefined if no
value passed)
FunctionDeclaration the Function object
variableDeclaration undefined
As an example:
(function(){
g = "Hola";
function g() { ; };
alert(g +", "+typeof g);
})();
1. skipped first pass
2. a property with identifier - g - and [[value]] - function() { ; } -
added to Variable object
3. skipped first pass.
1. assign g to value "Hola"
2. skipped
3. alert statement
alert(typeof g);