简体   繁体   English

自执行匿名函数语法

[英]Self Executing Anonymous Function Syntax

I often write my JS self executing anonymous functions like this 我经常写这样的JS自执行匿名函数

(function(){})()

but the other day I saw this, in somebody's code 但是前几天我用某人的代码看到了

(function(){}())  

what's the difference, and is one recommended over the other? 有什么区别,一个推荐另一个推荐吗?

(function(){}());

I recommended this one, because it makes more sense. 我推荐了这个,因为它更有意义。

You have your function function(){} then you append the () to execute it, then you wrap the whole thing in () to specify that it's an expression. 您具有函数function(){}然后附加()以执行它,然后将整个内容包装在()以指定它是一个表达式。 That is done so the js interpreter will not define it as a function declarations, but as a function expression. 这样做是为了让js解释器不会将其定义为函数声明,而是定义为函数表达式。

But it doesn't matter, it will execute properly, so it's a personal taste problem. 但这无关紧要,它将正确执行,因此这是个人喜好问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM