-
-
Save mingfang/5c63f7db00cdfe73ce53cffbbe45ef6c to your computer and use it in GitHub Desktop.
dynamic javascript eval
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
export default function f(body) { | |
const code = ` | |
with(this){ | |
return(async function(){ | |
${body} | |
}).call(this) | |
} | |
` | |
return new Function(code) | |
}; | |
(async function main() { | |
const body = 'return f()' | |
const context = { | |
foo: { one: 'one-two' }, | |
f: () => ("i'm a function") | |
}; | |
console.log('f', await f(body).call(context)) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment