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

浙公网安备 33010602011771号