Skip to content

Instantly share code, notes, and snippets.

@mingfang
Created September 24, 2024 11:56
Show Gist options
  • Save mingfang/5c63f7db00cdfe73ce53cffbbe45ef6c to your computer and use it in GitHub Desktop.
Save mingfang/5c63f7db00cdfe73ce53cffbbe45ef6c to your computer and use it in GitHub Desktop.
dynamic javascript eval
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