Should myFunc = function () {} evaluate Javascript right away?

Leave a comment if I am wrong: in my memory I was thinking about the difference between the two styles of creating a function:

function myFunc(){}; 

and

 myFunc = function(){}; 

- The first evaluation immediately, and later waiting for a function call.

so I got this code in this article about a global variable attached to a window object:

 function setGloabalVariable(){ window.variable = '1'; } 

after loading the page, why is the variable still undefined?

+4
source share
3 answers

The difference between the two types of declarations is that the first is the named operator of the function, the second is the anonymous expression of the function. Neither of these are automatically executed at the point of declaration (you can think of IIFE).

 // Create a symbol named "my_named_function" // that points at the named function "my_named_function" function my_named_function() { } // Create a symbol named "my_anonymous_func" // that points to the *unnamed* function <anonymous function> var my_anonymous_func = function() { } 

Both my_named_function and my_anonymous_func not executed. Both can be done by calling them:

 my_named_function(); my_anonymous_func(); 

The IIFE function (Executed Executed Expression expression) works a little differently:

 (/* define a function */ function() { })(/* and *immediately* execute it */); 

I recommend reading the excellent kangax article on function expressions and operators for more information on this.

+3
source

function myFunc(){} is a global function literal; it does not immediately "start", but can refer to forward and backward . Value:

 myFunc(); // this will run function myFunc(){} 

How in:

 myFunc(); // this will **not** run myFunc = function (){} 

The difference with the second function is that even if you did not declare var myFunc = . It becomes a global variable (also known as a built-in anonymous function), different from a global built-in function, because it must be above a certain number (backward link) to use.

 function setGloabalVariable(){ window.variable = '1'; // this could have been written // variable = '1'; var variable = '1'; // unless this function is called in some global scope, the closure won't // bring out this variable } 

An anonymous function runs instantly, and I think this is what you are thinking about (but not in your OP).

(function () { }());

An article about autonomous anonymous functions

Anonymous functions created a local / internal scope. Great for creating namespaces and storing objects in a namespace.

0
source

A named function is created and assigned at compile time, so it exists before the code runs. Example:

 x(); // This works, as the function below already exists function x() {} 

An anonymous function can be created at compile time (implementation dependent), but a value is assigned to a variable at runtime. Example:

 x(); // This doesn't work, as the value is not assigned yet x = function(){}; x(); // Here it works 

In both cases, you must actually call the function for the code inside the executable function.

0
source

All Articles