Why does copying eval change its behaviour?


According to the rollupjs documentation:


Simply 'copying' eval provides you with a function that does exactly the same thing, but which runs in the global scope rather than the local one:

</blockquote> var eval2 = eval; (function () { var foo = 42; eval('console.log("with eval:",foo)'); // logs 'with eval: 42' eval2('console.log("with eval2:",foo)'); // throws ReferenceError })();

Can anyone explain exactly how this works? I haven't been able to find anything specific about eval() in the ECMAScript spec.

Perhaps eval is not actually a function, but a magic token that gets replaced by a function that executes code in that scope, a bit like this:

var eval2 = CREATE_EVAL(CURRENT_SCOPE); (function () { var foo = 42; CREATE_EVAL(CURRENT_SCOPE)('console.log("with eval:",foo)'); // logs 'with eval: 42' eval2('console.log("with eval2:",foo)'); // throws ReferenceError })();

However, since we're dealing with modules here (commonJS or ES6), this would imply that eval2 actually runs in the module scope. The rollupjs documentation specifically says it runs in global scope - I figured this was just an oversight, but when I tested this, it does actually appear to run in the global scope:

var x = 1; var eval2 = eval; (function () { eval2('console.log("with eval2:", x)'); // throws ReferenceError })();

That's very confusing! How in the world does this work? Why does copying the reference to eval change its behaviour so drastically?


Because the ECMAScript 5 language specification says that direct references to eval will execute in the local scope, while indirect references will use the global scope.

A more English-friendly description can be found on MDN:


If you use the eval function indirectly, by invoking it via a reference other than eval, as of ECMAScript 5 it works in the global scope rather than the local scope. This means, for instance, that function declarations create global functions, and that the code being evaluated doesn't have access to local variables within the scope where it's being called.




  • How can I read IBM jit dump file
  • sql query to solve the following
  • How to detect the area of touch in open gl es in android?
  • Fast and efficient way to expand a dataset in R
  • How to fetch Salesforce Attachment file content with django-salesforce?
  • Controling HTML5 video with jQuery
  • Maven compilation complication due to complex folder structure
  • dynamically skip top blank rows of excel in python pandas
  • how do you uninstall an xampp installation that refuses to uninstall?
  • How to recreate a virtual env in python
  • GBP pound symbol appearing as uknown char in shop
  • Repeating Texture on Model/Mesh
  • How to parse this JSON in PHP?
  • Best way for multi-language sites virtual Directories
  • Difference between Canvas and Grid to Move, Resize, Rotate UIElements
  • Using Windows.Automation, can I locate an AutomationElement by regex?
  • Wait for a background process to end with SwingWorker from a different class
  • PhoneGap FileReader/readAsDataURL Not Triggering Callbacks
  • How to get the remaining session timeout using SessionState?
  • Android How to call a method multiple times with a delay between them
  • How to load PDFs from Documents Directory?
  • Proper Form of API request to Blogger using Java/App Engine -error 401
  • sql server back restore - login failed
  • How to get Attachment value from “$File” Item? using C# (Lotus Notes)
  • Unable to start a WebView from an AsyncTask
  • C# code can't “see” the methods in my C++ dll
  • All Event listing on specified date in Google Calender api (V3) in java?
  • How to run chrome.tabs.insertCSS from the background page on each page?
  • Google Spreadsheet Script to Blink a range of Cells
  • how to snap two objects in runtime in unity?
  • Another “Cannot make static reference…” Question
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular 4: Responsive Grid List
  • Write to .csv file with PHP (Commas in Data Error)
  • How to mutate multiple variables without repeating codes?