The following describes how to define functions in javascript: functiona () {} and afunction () {}. For more information, see, I hope it will help you to learn the function definition. Generally, the call results of the two functions are the same, but there are still differences.
Method 1:
The Code is as follows:
Function (){
Alert ('Old ');
}
Var B =;
Function (){
B ();
Alert ('new ');
}
A (); // memory overflow occurs in the browser.
Method 2:
The Code is as follows:
Function (){
Alert ('Old ');
}
Var B =;
Var a = function (){
B ();
Alert ('new ');
}
A (); // the browser will generate 'old' and 'new' in order'
Here we can clearly distinguish between the two methods. The order of definition is different.
First, the function a was not re-defined at the beginning and the function itself was executed in it.
In the second way, a = function () is not executed here. Code a in the function has been redefined. So the definition here is valid.
Supplement 1:
The Code is as follows:
Function (){
Alert ('Old ');
}
Var B =;
Function (){
B ();
Alert ('new ');
}
During Compilation: a is first defined as alert ("old") and then B (); alert ("new ");
Runtime: B = function a () {B (); alert ("new") ;}. in this case, B and a are the same, and B is called directly in the function body, both a call and B call results are the same, resulting in stack overflow.
On the other hand
The Code is as follows:
Function (){
Alert ('Old ');
}
Var B =;
Var a = function (){
B ();
Alert ('new ');
}
During Compilation: a is defined as alert ("old ")
Runtime: B = function a () {alert ("old")}; a = function () {B (); alert ("new ")}; at this time, the function body of B does not include any of AB. a only calls B... stack Overflow will not occur in any way...
Supplement 2:
Generally, the first method is used to avoid code pollution, but if you need to retain the original function, you need to use the second method. Both methods comply with w3c.
In fact, the first writing method was only available later, which was optimized.