本文介绍了我们需要JavaScript函数表达式的额外括号的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么/什么时候在函数表达式中需要额外的括号。这两个代码返回相同的结果。任何需要extra()的上下文?

  //无括号
var y = function(x){返回x * x; }(4)+ 1;
console.log(y);

//括号
var y =(function(x){return x * x;}(4))+ 1;
console.log(y);

感谢

解决方案

当解析器处于期望语句而不是表达式的状态时,您只需要括号(这不是额外的,因为您需要它们!)。所以如果你这样想:如果在这里发表语句,我可以开始一个,如果答案是是的话我可以然后你需要括号。 p>

原因是当解析器期待一个语句时,如果它看到关键字函数,它假定以下是一个函数声明,而不是一个函数表达式。



为了使解析器期望一个表达式,我们不 使用括号,任何将使解析器期望表达式的令牌工作。



所以你在 = 之后不需要它们,例如(你的例子),因为解析器已经在期望在赋值的右边的表达式。类似地,您不需要在属性初始化程序中的之后:

  var obj = {
foo:function(){/ * ... * /}
};

...或将函数表达式作为参数传递给函数时:

  foo(function(){
/ * ... * /
});

...那种事情。



但是你需要这些:

  doSomething(); 
function(){/ * ... * /}(); //< == Fails

...因为当它看到函数,解析器认为这将是一个函数声明,所以:

  doSomething(); 
(function(){/ * ... * /})(); //< == Works

  doSomething的(); 
(function(){/ * ... * /}()); //< == Works
// Subtle diff here ---- ^^^

或者一些更模糊的例子:

  doSomething(); 
+ function(){/ * ... * /}(); //< == Works

doSomething();
!function(){/ * ... * /}(); //< == Works

doSomething();
〜function(){/ * ... * /}(); //< == Works


Why/when do we need extra brackets in function expressions. These two pieces of code return same result. Any context where we need the extra ()?

// without brackets
var y = function (x) { return x * x; }(4) + 1;
console.log(y);

// with brackets
var y = (function (x) { return x * x; }(4)) + 1;
console.log(y);

Thanks

解决方案

You only need the parentheses (which are not "extra," since you need them!) when the parser is in a state where it's expecting a statement rather than an expression. So if you think in terms of: "Could I start an if statement here?", if the answer is "Yes, I could" then you need the parens.

The reason is that when the parser is expecting a statement, if it sees the keyword function, it assumes what follows is a function declaration, not a function expression.

To make the parser expect an expression, we don't have to use parens, any token that will make the parser expect an expression works. More information on that in this other answer.

So you don't need them after an =, for instance (your example), because the parser is already expecting an expression on the right-hand side of the assignment. Similarly you don't need them after the : in a property initializer:

var obj = {
    foo: function() { /* ... */ }
};

...or when passing a function expression into a function as an argument:

foo(function() {
    /* ... */
});

...that sort of thing.

But you need them here:

doSomething();
function() { /* ... */}();   // <== Fails

...because when it sees function, the parser thinks it's going to be a function declaration, so:

doSomething();
(function() { /* ... */})(); // <== Works

Or

doSomething();
(function() { /* ... */}()); // <== Works
// Subtle diff here ----^^^

Or some more obscure examples:

doSomething();
+function() { /* ... */}();  // <== Works

doSomething();
!function() { /* ... */}();  // <== Works

doSomething();
~function() { /* ... */}();  // <== Works

这篇关于我们需要JavaScript函数表达式的额外括号的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:50
查看更多