适用范围:

1. 作用:

  • 动态创建函数,用代码写代码
  • 动态执行代码

2. 缺点:

  • 会产生安全漏洞,第三方可能往Funtion构造函数中注入恶意代码

3. 场景

语法:

new Function('参数1','参数2','函数体')

实例:

let data = new Function(`return 'hello '+true || false`)()
console.log(data)//hello true
 
let data = new Function('arg1', 'arg2', "return arg1+arg2")(1, 2)
console.log(data)//3