对于IIFE,这些微小的语法变化有区别吗? [英] Difference in these tiny syntax variations for an IIFE?

查看:61
本文介绍了对于IIFE,这些微小的语法变化有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我看到了:

(function() {
    alert("hi");
})();

有时我会看到:

(function() {
    alert("hi");
}());

请注意函数对象的结束括号的位置.

Note the placement of the closing paren for the function object.

有什么区别?我不知道.出于任何原因都可以选择吗?

What is the difference? I can't figure it out. Is either preferable for any reason?

此外,这不起作用:

function() {
    alert("hi");
}();

这似乎很奇怪,因为如果用括号括起来,它是有效的,如示例2所示.我不明白为什么用括号括起来会在这方面发生任何变化.

Which seems weird, since it is valid if wrapped in parentheses, as in example 2. I don't understand why wrapping it in parentheses changes anything in that respect.

推荐答案

#1和#2之间完全没有100%的差异.

100% no difference between #1 and #2, at all.

#3很棘手.

您可以这样声明函数:

function funcName(){}

function funcName () { }

JS实际上将遍历您的代码,并挑选所有以这种方式编写的函数声明(在您当前的作用域内),然后再查看该作用域中的其余代码.

JS will actually go through your code and pick out all of the function declarations which are written like that (within your current scope), before it even looks at the rest of the code in that scope.

例如,如果您写:

(function () {
    var myVar = setVar();

    function setVar () { return 1; }
}());

之所以有效,是因为JS进入了该范围,选择了函数声明,然后查看了其余的范围(这就是为什么它不会抛出 undefined不是函数的原因-您的错误).

It works, because JS entered that scope, picked up the function declaration, and then looked at the rest of your scope (which is why it doesn't throw an undefined is not a function reference-error at you).

所以写:

function () { }();

JS现在将其视为

function <name-is-missing> () { }
(/* evaluate whatever is in here, when you're ready to run through the scope */);

当然,JS绝不会达到()的程度,因为没有名称的声明很重要.

Of course, JS will never make it as far as the () because a declaration with no name is a big deal.

请问哪位是家长:

(/*评估此处的内容*/);

(/* evaluate what's in here */);

#1和#2之间的细微差异是这样(实际差异为0%):

The subtle difference between #1 and #2 is this (real-world difference -- 0%):

// on the inside
var end = (/*evaluate*/function () { return 1; }()/*1*/ /*return*/);
console.log(end); // 1;

// on the outside
// step-1
var end = (/*evaluate*/ function () { return 1; } /*return*/);
console.log(end); // function () { return 1; }

// step-2
end();

...除了我作弊.在JS中,表达式的整个链都是在分配左手之前求值的...

...except that I cheated. In JS, the entire chain of the expression is evaluated before the left-hand is assigned...

var end = (function () { return 1; })/*function(){}*/()/*1*/;
console.log(end); // 1


There are other ways of showing the JS parser that the function is not a declaration:

var bob = function () { return "Bob"; }();
// it's on the right-hand side, so it must be an expression,
// and will be run inline with the rest of the scope

!function () { return "Nobody will get this"; }();
// JS will evaluate whatever's behind the `!` to determine its truthiness
// (and then invert it)

+function () { return "I am not a number!"; }();
// same deal here, as JS attempts to cast the final value to a number

这篇关于对于IIFE,这些微小的语法变化有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆