Created
November 7, 2014 03:15
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 eval(str, context, expose) { | |
var exposeKeys = []; | |
var exposeValues = []; | |
var i; | |
for (i in expose) { | |
if (Object.hasOwnProperty.call(expose, i)) { | |
exposeKeys.push(i); | |
exposeValues.push(expose[i]); | |
} | |
} | |
return (new Function( | |
'return function(' + exposeKeys + ') { return function() { ' + str + ' }.bind(this) }' | |
))().apply(context, exposeValues); | |
} | |
var ctx = {}; | |
var expose = { | |
up: function(str) { | |
return str.toUpperCase(); | |
} | |
}; | |
var f = eval('console.log(this, arguments); return up("aaa")', ctx, expose); | |
console.log(f.toString()); | |
var t = f('Look! Even has args!'); | |
console.log(t); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment