difference between Function and new Function

前端 未结 4 1181
萌比男神i
萌比男神i 2020-12-11 03:37

I sometimes see people doing this Function(\'alert(\"hi\")\') and sometimes they do new Function(\'alert(\"hi\")\')

Is there a difference b

4条回答
  •  生来不讨喜
    2020-12-11 04:21

    Invoking the Function constructor as a function (without using new operator) has the same same effect as invoking it as constructor.

    That means it is same to code if you do it:

    var result1 = new Function('a', 'b', 'return a + b')
    var result2 = Function('a', 'b', 'return a + b')
    

    result1 and result2 are same.

提交回复
热议问题