typeof Function.prototype 是 function 为什么不可以当作构造函数

typeof Function.prototype 是 function 为什么不可以当作构造函数 与前者有什么

哆啦的时光机
浏览 467回答 1
1回答

呼如林

是可以当做构造函数来用的var adder = new Function('a', 'b', 'return a + b');// Call the functionadder(2, 6);// > 8区别的话,用构造函数生成的函数不会有闭包,他们只能访问自身作用域和全局作用域声明的变量和函数。其他详细的地方参考MDNFunctions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.var x = 10;function createFunction1() {    var x = 20;    return new Function("return x;"); // this |x| refers global |x|}function createFunction2() {    var x = 20;    function f() {        return x; // this |x| refers local |x| above    }    return f;}var f1 = createFunction1();console.log(f1());          // 10var f2 = createFunction2();console.log(f2());          // 20
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript