[Js] function declaration and call

Normal function declaration and call

function A () { 
  Alert (
'IIFE' );}
// The following is a function call A ();

In a statement to understand what is behind the function of a plus () function can be called up

So I wrote the following empathy (a bit small error, error)

function a(){ 
    alert('IIFE'); }()

But we executed according to the above in the console found a mistake

Because this code to confuse the function declarations and function calls to functions declared in this way  a, it should  a(); call the way.

However brackets is different, it will be a function declaration transformed into an expression parser no longer function declarations in the manner of a handler, but as an expression processing function, and therefore only a function to execute when it is in the program It can be accessed. So, any way to eliminate between function declarations and function expressions ambiguity, the parser can be correctly identified . Therefore, the assignment logic, even a comma, various operators can tell the parser, this is not a function declaration, it is a function expression. Further, the function of the unary can be counted disambiguation fastest way, only one of exclamation mark, do not care if the return value, which unary are valid

! function () {Alert ( 'iifksp')} () to true // 
+ function () {Alert ( 'iifksp')} () // NaN3 
-function () {Alert ( 'iifksp')} () // NaN3 
~ function () {Alert ( 'iifksp')} () -1 // 

// recommended the following
(function () {
  alert('iifksp'})()        

One to choose:

In response to these unary operators, in the end with what is good, the test found () is the most superior performance, but the difference is not obvious special

So for a library, it can not see what effect a few such symbols is so common with + -! All OK, the code depends on personal habits, of course, better to use ().

Guess you like

Origin www.cnblogs.com/htybky/p/11521793.html