这些自执行匿名函数(又名IIFE)实现之间有什么区别 [英] What is the difference between those self-executing anonymous function (aka IIFE) implementation

查看:98
本文介绍了这些自执行匿名函数(又名IIFE)实现之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多书中/ 博客文章自调用匿名函数模式的方式如下:

In many books / blog posts the self invoking anonymous function pattern is written like this:

(function() {
  var foo = 'bar';
})();

但运行 JSLint 就此给出了这个错误:

However running a JSLint on this gives this error:


将调用移动到包含该函数的parens中。 / p>

Move the invocation into the parens that contain the function.

例如改变它的工作原理:

e.g. changing it to this works:

(function() {
  var foo = 'bar';
}());

问题


  1. 为什么第一个实现对JSLint来说不够好?有什么区别?

  2. 首选表格是什么? JSLint永远是对的吗?

  3. 为什么它可以工作?在所有 function(){}()之后抛出语法错误:意外的令牌(
    但是用parens包装它会使它突然工作?例如 function(){}() - 工作正常


    (毕竟这是JavaScript,而不是Lisp,所以什么是包装parens对ohterwise语法错误的影响?)

  1. Why is the first implementation not good enough for JSLint? What are the differences?
  2. What is the preferred form? is JSLint always right?
  3. Why does it work? after all function(){}() throws a SyntaxError: Unexpected token (
    But wrapping it with parens makes it all of a sudden work? e.g. (function(){}()) - works fine
    (After all this is JavaScript, not Lisp, so what is the effect of the wrapping parens on the ohterwise syntax error?)

编辑:这有点像对此进行跟进(我不会说完全重复): JSLint错误:将调用移动到包含函数的parens中,所以我的主要问题是#3,为什么它可以工作?

EDIT: this is somewhat of a followup to this (I would not say exact duplicate though): JSLint error: "Move the invocation into the parens that contain the function", so my main question is #3, why does it work at all?

推荐答案

我不知道Crockford的意见是如何制定的,但我可以解释为什么包裹在parens的工作中。

I don't know how Crockford's opinions were developed, but I can explain why wrapping in parens works.

JavaScript中的函数 function(){...} 语法可以代表两个不同的东西:一个函数声明或函数表达式。

The function function() { ... } syntax in JavaScript can represent two different things: a function declaration or a function expression.

函数声明是一个语句,用于在指定名称下定义当前作用域内的函数。

A function declaration is a statement that defines the function within the current scope under the specified name.

function example() { 
    alert("Hello World");
}

example();

函数表达式是表达式,其计算结果为新函数实例。

A function expression is an expression that evaluates to a new Function instance.

var secondExample = function example() {
    alert("Hello World");
};

secondExample();
example(); // <-- throws an Error: example is not defined.

语法的出现是函数声明还是函数声明取决于解析器的期望。 JavaScript的解析器很简单。它不会向前看并注意到该函数后跟(),因此它应将其视为表达式。它只是在行的开头看到函数,因此将其视为一个语句,当它后跟()时会导致语法错误。当你将它包装在括号中时,解析器会期望一个表达式,并且它可以正常工作。

Whether a occurrence of the syntax is a function declaration or function statement depends on what the parser was expecting. JavaScript's parser is simple. It won't look ahead and notice that the function is followed by (), therefore it should treat it as a expression. It just sees the function at the beginning of a line, and therefore treats it as a statement, which causes a syntax error when it's followed by (). When you wrap it in parentheses, the parser is instead expecting an expression, and it works.

用括号括起来(无论你把它放在哪里)都是最清晰的方法这个,但任何导致解析器期望表达式的东西都会起作用。例如,按位NOT运算符

Wrapping in parentheses (wherever you place them) is the the clearest way to do this, but anything that causes the parser to expect an expression will work. For example, the bitwise NOT operator ~:

~function() {
    alert("Hello World");
}();

这篇关于这些自执行匿名函数(又名IIFE)实现之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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