new Function()语法

let func = new Function(arg1, arg2, ..., body);

new Function('a', 'b', 'return a + b'); // 基础语法


但是当使用new Function()创建函数时,其[[Environment]]不是引用当前的词法环境,而是引用全局环境。

function getFunc() {
  let value = "test";

  let func = new Function('alert(value)');

  return func;
}

getFunc()(); // error: value is not defined
posted @ 2021-10-14 10:25  果果1024  阅读(58)  评论(0)    收藏  举报