立即调用函数表达式:括号中的位置? [英] Immediately Invoked Function Expression: Where to put the parenthesis?

查看:89
本文介绍了立即调用函数表达式:括号中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过IIFE写的:

(function() {
    console.log("do cool stuff");
})();

以及:

(function() {
    console.log("do more cool stuff");
}());

它们似乎在我使用它们的任何环境中都是一样的,不过在我遇到的情况下被告知一种方式是正确的,另一种方式是错误的,反之亦然。有没有人有任何坚实的理由或逻辑,因为它是一个订单而不是另一个订单?在某些情况下,在函数体关闭之后但在调用括号之前或之后但在最后的右括号之前可能会有更多的事情发生?我主要在一个Angular模块闭包中使用它们,似乎无法找到任何真正的理由去某种方式,想知道是否有人有不同的经验。

They seem to work the same in any context I've used them, though in cases I've been told one way is right and the other is wrong, vice versa. Does anyone have any solid reason or logic as to it being written one order over the other? Is there some cases where there could be potentially more going on after the function body closes but before the invoking parenthesis come into play, or after but before that final closing parenthesis? I've mostly used these in an Angular module closures and can't seem to find anything on any real reason to go one way or the other, wondering if anyone had different experience.

推荐答案

简短回答:没关系,只要你把它们放在那里。

Short answer: It doesn't matter, as long as you put them there.

长答案:它没有除了箭头函数的情况之外,因为对于JavaScript来说唯一重要的是它们就在那里。原因是语言规范定义了声明只能以函数关键字开头,如果声明命名的函数。因此,定义类似IIFE的规范是违反规定的:

Long answer: It doesn't matter except in the case of arrow functions, because for JavaScript the only important thing is that they are there. The reason for this is that the language spec defines that a statement must only start with the function keyword if you declare a named function. Hence, it is against the spec to define an IIFE like that:

function () {}();

此解决方法是将整个事物包装在括号中,以便语句不以函数关键字了。您可以使用

The workaround for this is to wrap the entire thing in parentheses, so that the statement does not start with the function keyword anymore. You achieve this by using

(function () {})();

以及使用:

(function () {}());

您选择的是完全随意的,因此取决于您。

Which one you choose is completely arbitrary and hence up to you.

我(个人)将括号放在函数周围,而不是在调用周围,即像这样:

I (personally) put the parentheses around the function, not around the call, i.e. like this one:

(function () {})();

原因:我想使用最小部分的代码包装在只需要的东西中技术原因,这是功能定义,而不是通话。除了规范说你不能以这种方式定义一个函数,它不是调用函数。因此,我认为更清楚的是你是否包装了定义,而不是电话。

Reason: I want to use the smallest portion of code to be wrapped in something that is only needed for technical reasons, and this is the function definition, not the call. Apart from that the spec says that you can not define a function in that way, it is not about calling the function. Hence, I think it's more clear if you wrap the definition, not the call.

编辑
但是,如果是es6中的箭头函数,调用必须在包装器之外。它位于包装器内部会导致调用的左括号出现意外的令牌错误。关于为什么会出现这种情况的机制尚不完全清楚。

edit However, in the case of the arrow function in es6, the invocation must be outside of the wrapper. It being inside the wrapper causes an unexpected token error on the opening parenthesis of the invocation. Not totally clear on the mechanics of why this is so.

这篇关于立即调用函数表达式:括号中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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