Using eval to execute functions

前端 未结 6 1228
太阳男子
太阳男子 2020-12-21 20:18

There is something I don\'t understand about how eval works. Suppose I have a function foo:

function foo() {
    console.log(\"test\");
}

A

6条回答
  •  天命终不由人
    2020-12-21 20:25

    Put brackets around your function

    eval("(function foo() { console.log(\"foo\"); })()");
    

    It is like calling

    (function foo() { console.log("foo"); })()
    

提交回复
热议问题