new Function()和new Function()()之间的区别

时间:2019-06-21 04:30:36

标签: javascript function constructor parentheses

我遇到了一些代码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还是双括号的实现在不同的浏览器中有所不同?

1 个答案:

答案 0 :(得分:2)

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)();

相关问题