Martin said:
You might want to check the return value of that delete x,
knowing that is essential I think to understand what the
alert later outputs. It seems the implementations don't
delete x (i.e. delete x yields false), that is why
those give the function as the result.
That seems odd to me and looks like a bug in the handling of
the eval call with the function declaration.
That certainly is an odd result. JavaScript(tm) has function statements
in addition to having the function expressions and function declarations
required by ECMA 262, which (not being formally specified themselves)
may account for this outcome. But the string arguments to - eval - are
supposed to be handled as an ECMAScript 'Program' and I don't see how
the contents of the evaled string could be anything but a function
declaration in that context.
That seems to be the right result if the delete x yielded true.
The specification of the var statement says:
"If the variable statement occurs inside a FunctionDeclaration,
the variables are defined with function-local scope in that
function, as described in section 10.1.3. Otherwise, they are defined
with global scope (that is, they are created
as members of the global object, as described in section
10.1.3) using property attributes { DontDelete }."
One could read that as saying that only a global variable
declaration should create a property with "DontDelete",
One could read it that way in isolation, but taken with section 10.2.3,
where for "Function code" it says "Variable instantiation is performed
using the activation object as the variable object and using property
attributes { DontDelete }", it would be difficult to sustains the
"only".
in that case your expectation of the result of your test
case showing "global" seems right but I a bit doubtful
of my analysis with all implementations seemingly agreeing on setting
"DontDelete" on the local var statement too.
Yes, the DontDelete on the function local variable definition is
required. The question here, and the bug(s) here, are with variable
instantiation for eval. For "Eval Code" ECMS 262 says "variable
instantiation is performed using the calling context's variable object
and using empty property attributes", so no DontDelete. And section
10.1.3 says, for "FunctionDeclaration" that "If the variable object
already ahs a property with this name, replace its value and
attributes." So the - var x - creates an - x - property of the variable
object which does have the DontDelete attribute during variable
instantiation for the execution of the function, but the variable
instantiation for the - eval - call should _replace_ both the value of
that - x - property and its attributes, and the replacement attributes
are empty so the final sate of the variable object's- x - property after
the eval call should be its having no DontDelete attribute. The - x -
property of the variable object should then be deletable so the - delete
x; - expression should delete it (removing the property form the
variable object), and unmask the global - x - variable as a consequence.
Let's hear some other opinions.
There certainly are bugs here, but not particularly important ones.
Using - eval - for its divergent attribute setting does not seem to have
any usefully exploitable applications.
Richard.