函数名称前的JavaScript加号

本文翻译自:JavaScript plus sign in front of function name

I've been looking on info about self-invoking functions, and somewhere I stumbled on this notation: 我一直在寻找有关自调用函数的信息,在某个地方我偶然发现了这种表示法:

+function(){}

Can someone explain to me what the + sign in front of the function means/does? 有人可以向我解释该功能前面的+号是什么意思吗?


#1楼

参考:https://stackoom.com/question/tymg/函数名称前的JavaScript加号


#2楼

It forces the parser to treat the part following the + as an expression. 它强制解析器将+的部分视为表达式。 This is usually used for functions that are invoked immediately, eg: 通常用于立即调用的函数,例如:

+function() { console.log("Foo!"); }();

Without the + there, if the parser is in a state where it's expecting a statement (which can be an expression or several non-expression statements), the word function looks like the beginning of a function declaration rather than a function expression and so the () following it (the ones at the end of the line above) would be a syntax error (as would the absense of a name, in that example). 如果没有+在那里,如果解析器是在它期待一个语句(可以是一个表达式或几个非表达式语句),字的状态function看起来像函数声明的开始,而不是一个函数表达式等等()后面的()句号(上面一行的末尾)将是语法错误(在该示例中,缺少名称也是如此)。 With the + , it makes it a function expression, which means the name is optional and which results in a reference to the function, which can be invoked, so the parentheses are valid. 使用 + ,使其成为函数表达式,这意味着名称是可选的,并且导致对该函数的引用,该引用可以被调用,因此括号是有效的。

+ is just one of the options. +只是选项之一。 It can also be - , ! 也可以是- ! , ~ , or just about any other unary operator. ~或几乎其他任何一元运算符。 Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically): 或者,您可以使用括号(这是更常见的,但是在语法上既不正确,也不正确):

(function() { console.log("Foo!"); })();
// or
(function() { console.log("Foo!"); }());

#3楼

Subsidiary to @TJCrowder's answer, + is usually used to force numerical casting of a value as this SO answer explains . 附属于@TJCrowder的答案, 如该SO答案所解释+通常用于强制数值的强制转换。 In this instance it is called the 'unary plus operator' (for ease of googling). 在这种情况下,它称为“一元加号运算符”(为便于谷歌搜索)。

var num = +variant;

So in front of a function it can be a way to force the function's result to be interpreted as a number. 因此,在函数前面可以采用一种方法来强制将函数的结果解释为数字。 I doubt it happens yet, but theoretically the JIT could use that to compile the function as a numerical-only function etc. However, to prevent the unary plus being a concatenation when used in a larger expression, you would need parentheses: 我怀疑它是否会发生,但是从理论上讲,JIT可以使用它来将函数编译为仅数字函数等。但是,为了防止一元加号在较大的表达式中使用时被串联,您需要使用括号:

blah + (+(function(){ var scope; return "4"; })());

#4楼

So the short answer is that it prevents a syntax error, by using the function results in one way or another. 因此简短的答案是,通过以一种或另一种方式使用函数结果,可以防止语法错误。

You can also instruct the engine that you're not even interested in the return value by using the void operator: 您还可以使用void运算符指示引擎您甚至对返回值都不感兴趣:

void function() { console.log("Foo!"); }();

Of course, putting braces around the whole thing also serves that purpose. 当然,将括号放在整个对象上也可以达到此目的。

发布了0 篇原创文章 · 获赞 73 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105366866