Created
November 27, 2015 11:10
-
-
Save butchi/2d8d1a583999744d9c27 to your computer and use it in GitHub Desktop.
Functionによるevalの代替 ref: http://qiita.com/butchi_y/items/d6024f81a9eda826fea0
This file contains hidden or 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
mathEval("pow(2, 10), alert('あぶない!')")(); |
This file contains hidden or 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 mathEval(expr) { | |
return Function('return Math.' + expr); | |
} | |
var f = mathEval("pow(2, 10);"); | |
var result = f(); | |
console.log(result); |
This file contains hidden or 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 myEval(expr) { | |
Function(expr)(); | |
} | |
myEval("alert('Thanks,'); alert('world!');"); |
This file contains hidden or 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 wrappedEval(expr) { | |
(function() { | |
eval(expr); | |
})(); | |
} | |
wrappedEval("alert('Thanks,'); alert('world!');"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment