Last active
August 29, 2015 13:57
-
-
Save jameswomack/9501178 to your computer and use it in GitHub Desktop.
Passing a string to `setTimeout` in browsers results in `eval` being used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logPIAfterDelay() { | |
setTimeout(function () { | |
console.log(Math.PI); | |
}, 1000); | |
} | |
function logPIAfterDelayEval() { | |
setTimeout("console.log(Math.PI)", 1000); | |
} | |
function logPIAfterDelayEvalStrict() { | |
'use strict'; | |
setTimeout("console.log(Math.PI)", 1000); | |
} | |
/* | |
All three of these work in modern browsers (not in node though). Result in Node.js: | |
timers.js:124 | |
first._onTimeout(); | |
^ | |
TypeError: Property '_onTimeout' of object [object Object] is not a function | |
at Timer.listOnTimeout (timers.js:124:15) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment