Behavior of `eval` in non strict mode.

Benjamin (Inglor) Gruenbaum inglor at gmail.com
Wed Jan 8 15:56:05 PST 2014


Thanks for the reply.

I'd actually expect `undefined` because function declarations does not
return anything. Converting it to a function expression kind of misses the
point since those are well... expressions :)

I've tried looking in all the relevant places in the spec but still
couldn't unambiguously figure out which browser is 'correct'.


On Thu, Jan 9, 2014 at 1:53 AM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> looks rather an eval gotcha but I think Firefox is correct anyway. try `f
> = eval("(" + f + ")");` instead and it should produce what you expect (I
> guess)
>
> Regards
>
>
> On Wed, Jan 8, 2014 at 3:37 PM, Benjamin (Inglor) Gruenbaum <
> inglor at gmail.com> wrote:
>
>> I've recently run into this question in Stack Overflow:
>>
>> http://stackoverflow.com/q/21008329/1348195
>>
>> ```
>>
>>
>> function f() {
>>     f = eval("" + f);
>>     console.log("Inside a call to f(), f is: \n%s", f);}
>>
>> f();
>>
>> console.log("After a call to f(), f is: \n%s", f);
>>
>> ```
>>
>> What should the output of the following be?
>>
>> I expected `undefined` on both but that's because I'm used to strict
>> mode. IE/Chrome treat this differently from Firefox and to be honest when I
>> checked the spec it boiled down to which context is affected here.
>>
>> In IE/Chrome the eval is creating `f` inside the context of `f` acting
>> like a function declaration inside. In Firefox it's acting like it's
>> running in the global context.
>>
>> Which is correct?  I've tried to follow 10.4.2 (or 18.2.1 in the ES6
>> draft which is nice) but I still couldn't figure out what "if there is no
>> calling context means".
>>
>> Thanks,
>> Benjamin Grunebaum
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140109/fc90b9c9/attachment.html>


More information about the es-discuss mailing list