This is because after you assign `eval` to `neweval` you're putting an
indirect eval call inside `wrapper` rather than a direct eval call.  A
direct eval call executes in the scope of `wrapper` (and thus this refers to
localObj), but an indirect eval call executes in the global scope according
to ES5.  I think that means technically Chrome is 'correct', though perhaps
not expected.  All browsers seem to have slightly different behavior with
respect to direct/indirect evals.

There's a great article on this behavior here:
http://perfectionkills.com/global-eval-what-are-the-options/
and similar discussions on this list:
https://groups.google.com/d/topic/jsmentors/fyDQl-wVIDI/discussion

Martin
gameclosure.com


On Fri, Feb 25, 2011 at 3:14 PM, mcot <[email protected]> wrote:

> I have some sample code below that generates a different result
> between browsers.  I *think* chrome is doing the wrong thing, but I
> wanted to ask here first.
>
>
>            function func(param) {
>                console.log(param.x)
>            }
>
>            x = 100;
>
>            // this is obv. 100
>            console.log(this.x)
>
>            var localObj = new function() { this.x = 90; }
>
>            // this is obv. 90
>            console.log(localObj.x)
>
>
>            wrapper = function(st) {
>                eval(st)
>            }
>
>           // this should be 90
>            str = "func(this)"
>            wrapper.apply(localObj, [str])
>
>
>
>            neweval = eval; eval = undefined;
>
>            wrapper = function(st) {
>                neweval(st)
>            }
>
>            // this is 90 in firefox, but 100 in chrome...
>            str = "func(this)"
>            wrapper.apply(localObj, [str])
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to