函數名稱前的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. 當然,將括號放在整個對象上也可以達到此目的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章