Before the name of the JavaScript function plus

This translation from: 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: I was looking for information from the calling function, somewhere I stumbled across this notation:

+function(){}

Me someone EXPLAIN to the What CAN at The +Sign in Front of means at The function / does? Can someone explain to me the function of the previous +number is what that means?


#1st Floor

Reference: before https://stackoom.com/question/tymg/ plus JavaScript function name


#2nd Floor

Forces at The Parser to Treat IT at The following Part at The +AS AN expression The. It forces the parser +is regarded as part of the expression. This is usually used for functions that are invoked immediately, eg: usually call for immediate function, for example:

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

Without The +there, IF The Parser IS in A State WHERE IT apos expecting A Statement (Which CAN BE AN expression The or Several non-expression The statements), The Word functionlooks like The Beginning of A function Declaration Rather Within last A function expression The 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). If not +there, if the parser is looking forward to it a statement (can be an expression several non-expression or statement), status word functionlooks like a function declaration begins, rather than a function expression , and so ()the latter ()period (the end of the top row) would be a syntax error (in this example, the name is missing in this way). With at 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. Use + , making it a function expression, which means the name is selected, and results in a reference to the function, the reference may be invoked, and therefore is effective in parentheses.

+It is just one of the options. +Just one of the options. Also CAN BE IT -, ! may be - ! , ~, or the any OTHER Just About unary operator. , ~Or almost any other unary operator. Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically): Or, you can use parentheses (which is more common, but neither correct in syntax nor correct):

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

#3rd floor

@ TJCrowder apos answer to subsidiary, +IS usually Force Used to Numerical value A Casting of AS not Hesitate to the this SO answer . Affiliated @TJCrowder the answer, as this SO answer explained , +is typically used to cast force values. In this instance it is called the ' unary plus operator' (for ease of googling). In this case, it is called "unary plus operator" (for ease of Google search).

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. Therefore, the foregoing function may be employed in a method to force a function of the results 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: I doubt it whether it will happen, but theoretically, JIT compiler can use it to function as a digital-only function and so on. However, in order to prevent the series when unary plus in a larger expression, you need to use parentheses:

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

#4th floor

So the short answer is that it prevents a syntax error, by using the function results in one way or another. So the short answer is, by one or another way to use the function result, it is possible to prevent syntax errors.

Also at The Engine CAN Instruct by You that you're not interested in the even at The return value by a using at The voidoperator: You can also use voidoperator instructs the engine that you are not even interested in the return value:

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

Of course, putting braces around the whole thing also serves that purpose. Of course, the entire object on the brackets can also achieve this purpose.

Original articles published 0 · won praise 73 · views 560 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105366866