Difference between local assessment and global assessment

Please consider two pieces of code (the first fingerprints are “Local eval”, the second is “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 although globalEval === eval evaluates to true , globalEval and eval behave differently because they have different names. (An eval can only be local if it is spelled accurately by eval .)

How can I distinguish from two ratings? Is there a way to extract variable labels for output behavior?

+6
source share
1 answer

Interesting. But since you control where / when your link to eval defined, you can tell how to distinguish them. For example, there is an object that has a "function pointer" And something to indicate the area - if you define it, you know where you are standing.

those. myEvaluator.scope will give you information about where the scope of the eval was, and myEvaluator.eval can be used for evaluation.

+2
source

Source: https://habr.com/ru/post/907701/


All Articles