6

Please consider the two snippets of code (the first prints "Local eval", the second prints "Global eval"):

(function f() { 
    var x;
    try {
        eval("x");
        console.log('Local eval');
    }
    catch (e) {
        console.log('Global eval');
    }
}())

and

var globalEval = eval;
(function f() { 
    var x;
    try {
        globalEval("x");
        console.log('Local eval');
    }
    catch (e) {
        console.log('Global eval');
    }
}())

It turns out that even though globalEval === eval evaluates to true, globalEval and eval behave differently because they have different names. (An eval can only be local if it is precisely written eval.)

How can I distinguish to two evals? Is there are a way to extract variable labels to infer behaviour?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • @MattiVirkkunen: I'm trying to build an example of [this](http://stackoverflow.com/questions/7223517/pure-function-given-strictly-equal-arguments-yielding-non-strictly-equal-results). I'm close, but not quite there yet. – Randomblue Feb 05 '12 at 16:02
  • Why didn't you just stick with [your previous question](http://stackoverflow.com/questions/9145385/accessing-local-eval-inside-function-scope)? –  Feb 05 '12 at 16:06
  • @amnotiam: It's kind of different, and I got confused. I wish I could delete it. – Randomblue Feb 05 '12 at 16:11
  • 1
    This article explains it all: http://perfectionkills.com/global-eval-what-are-the-options/ – Šime Vidas Feb 05 '12 at 16:23

1 Answers1

2

Interesting. But since you're in control of where/when your reference to eval is defined, you get to say how to distinguish them. For example have an object that has the "function pointer" AND something to indicate the scope - if you define it, you know where you stand.

I.e. myEvaluator.scope would tell you info about where the eval scope was captuerd, and myEvaluator.eval could be used to evaluate.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144