From: Julian Turner on
Hi

I am trying to understand the meaning of "Strict mode eval code cannot
instantiate variables or functions in the variable environment of the
caller to eval" in ECMASCRIPT 5.

Does it mean that if you have something like:-

var v;
eval("v = 2");

v remains undefined / uninitialised?

Julian

From: Julian Turner on
On 3 Dec, 11:15, Julian Turner <julesb...(a)googlemail.com> wrote:
> Hi
>
> I am trying to understand the meaning of "Strict mode eval code cannot
> instantiate variables or functions in the variable environment of the
> caller to eval" in ECMASCRIPT 5.
>
> Does it mean that if you have something like:-
>
> var v;
> eval("v = 2");
>
> v remains undefined / uninitialised?
>
> Julian

Sorry

By which I mean:-

var v;
eval("v = 2; alert(v);"); // 2
alert(v); // undefined

Julian
From: "Michael Haufe ("TNO")" on
Quoted from the Final Draft of ES5 (10.4.2.1):

"The eval code cannot instantiate variable or function bindings in the
variable environment of the calling context that invoked the eval if
either the code of the calling context or the eval code is strict
code. Instead such bindings are instantiated in a new
VariableEnvironment that is only accessible to the eval code."
From: kangax on
Julian Turner wrote:
> On 3 Dec, 11:15, Julian Turner <julesb...(a)googlemail.com> wrote:
>> Hi
>>
>> I am trying to understand the meaning of "Strict mode eval code cannot
>> instantiate variables or functions in the variable environment of the
>> caller to eval" in ECMASCRIPT 5.
>>
>> Does it mean that if you have something like:-
>>
>> var v;
>> eval("v = 2");
>>
>> v remains undefined / uninitialised?
>>
>> Julian
>
> Sorry
>
> By which I mean:-
>
> var v;
> eval("v = 2; alert(v);"); // 2
> alert(v); // undefined

From what I can tell, this assignment should work. It is variable (and
function) *declarations* that specification is talking about.

In other words:

'use strict';
eval('var x;')
typeof x; // should be "undefined"

and:

'use strict';
eval('function x(){}');
typeof x; // should be "undefined" too

but:

'use strict';
var x;
eval('x = 2');
typeof x; // should be "number"

Also note that eval code is strict not only when it is called from
within strict code (as a direct call), but also when it begins with a
Use Strict Directive.

For example:

eval('"use strict";var x = 1;');
typeof x; // should be "undefined"

And of course it's worth mentioning that ES5 is still draft, so
potentially is a subject to change.

--
kangax
From: Julian Turner on
On 3 Dec, 14:38, kangax <kan...(a)gmail.com> wrote:
> Julian Turner wrote:
> > On 3 Dec, 11:15, Julian Turner <julesb...(a)googlemail.com> wrote:
> >> Hi
>
> >> I am trying to understand the meaning of "Strict mode eval code cannot
> >> instantiate variables or functions in the variable environment of the
> >> caller to eval" in ECMASCRIPT 5.
>
> >> Does it mean that if you have something like:-
>
> >> var v;
> >> eval("v = 2");
>
> >> v remains undefined / uninitialised?
>
> >> Julian
>
> > Sorry
>
> > By which I mean:-
>
> > var v;
> > eval("v = 2; alert(v);"); // 2
> > alert(v); // undefined
>
>  From what I can tell, this assignment should work. It is variable (and
> function) *declarations* that specification is talking about.
>
> In other words:
>
>    'use strict';
>    eval('var x;')
>    typeof x; // should be "undefined"
>
> and:
>
>    'use strict';
>    eval('function x(){}');
>    typeof x; // should be "undefined" too
>
> but:
>
>    'use strict';
>    var x;
>    eval('x = 2');
>    typeof x; // should be "number"
>
> Also note that eval code is strict not only when it is called from
> within strict code (as a direct call), but also when it begins with a
> Use Strict Directive.
>
> For example:
>
>    eval('"use strict";var x = 1;');
>    typeof x; // should be "undefined"
>
> And of course it's worth mentioning that ES5 is still draft, so
> potentially is a subject to change.
>
> --
> kangax- Hide quoted text -
>
> - Show quoted text -

Thank you kangax. That does make sense.
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: seethis VERY DIFFERENT
Next: regex help?