我遇到了一些代码here,其中以有趣的方式调用了Function构造函数:

var jscriptVersion; // ...some code
jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();


起初,我认为多余的括号是一个错误,但是在控制台中进行了一些测试之后,我认为这可能是获取返回值的捷径:

var a = new Function("return 'abc'");
var b = new Function("");
var c = new Function("")();
var d = new Function("return 'xyz'")();

console.log(a, typeof a);  // f anonymous() { return 'abc' } , function
console.log(b, typeof b);  // f anonymous() { } , function
console.log(c, typeof c);  // undefined , undefined
console.log(d, typeof d);  // xyz , string


我猜d在功能上等同于(除了new关键字之外):

var d2 = function() { return 'xyz' }(); console.log(d2); // xyz


但是,我再一次在非IIFE的函数表达式后面都没有看到括号,而且令我感到惊讶的是,定义d2的代码不会导致SyntaxError。

我查看了MDN,但找不到有关如何使用它的明确信息。

这是有效的JS,还是双括号的实现在不同的浏览器中有所不同?

最佳答案

new Function后面的括号将立即调用该函数。它与IIFE基本相同,不同之处在于被调用的函数是根据传递的字符串动态构建的-new Function返回一个函数,然后在紧随()之后立即运行。



var a = new Function("return 'abc'");
var b = new Function("");
var c = new Function("")();
var d = new Function("return 'xyz'")();

console.log(a, typeof a);  // f anonymous() { return 'abc' } , function
                           // because that is the function that was constructed from the `return 'abc'` string
console.log(b, typeof b);  // f anonymous() { } , function
                           // because the string passed was empty, so the function returned from new Function is empty
console.log(c, typeof c);  // undefined , undefined
                           // because an empty function, when invoked, does not return anything:
                           // x = (() => {
                           // })();
                           // x is undefined
console.log(d, typeof d);  // xyz , string
                           // because the constructed function's body returns the `xyz` string
                           // and the constructed function was invoked immediately





返回函数的任何函数在调用后都可以具有(),而不仅仅是new Function或IIFE。例如:



const makeFn = arg => () => console.log(arg);

const fnThatLogs3 = makeFn(3);
fnThatLogs3();

makeFn(4)();

关于javascript - new Function()和new Function()()之间的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56696859/

10-12 12:51
查看更多