Generally speaking, the call results of the two are the same, but there are still differences.
The first method:
function a(){
alert('old');
}
var b=a;
function a(){
b();
alert('new');
}
a();//The browser will experience memory overflow
The second method:
function a(){
alert('old');
}
var b=a;
var a=function(){
b();
alert('new');
}
a();//The browser will alert 'old' and 'new' in order
Here we can clearly distinguish the difference between the two methods. The order of definitions is different.
The first type is that at the beginning, a function was not redefined but it was executed in it.
In the second way, a = function () The code a that has not been executed in the function has been redefined. So the redefinition here is valid
Supplement 1:
function a(){
alert('old');
}
var b=a;
function a(){
b();
alert('new');
}
Compilation time: First a is defined as alert("old"), then it is defined as b(); alert("new");
Runtime: b = function a(){b();alert("new");}, at this time, b is the same as a, and b is called directly in the function body. The result is the same whether it is called from a or from b, resulting in stack overflow.
on the other hand
function a(){
alert('old');
}
var b=a;
var a=function(){
b();
alert('new');
}
Compilation time: a is defined as alert("old")
Runtime: b=function a(){alert("old")}; a=function(){b();alert("new")}; At this time, b's function body does not include any of ab, a only calls b... No stack overflow will occur regardless of whether it is...
Supplement 2:
Generally speaking, the first method is used to avoid code contamination, but if you need to keep the original function, you must use the second method. Anyway, both methods are in line with w3c.
In fact, the first writing method was only available later, and this writing method was optimized.
The first method:
Copy the codeThe code is as follows:
function a(){
alert('old');
}
var b=a;
function a(){
b();
alert('new');
}
a();//The browser will experience memory overflow
The second method:
Copy the codeThe code is as follows:
function a(){
alert('old');
}
var b=a;
var a=function(){
b();
alert('new');
}
a();//The browser will alert 'old' and 'new' in order
Here we can clearly distinguish the difference between the two methods. The order of definitions is different.
The first type is that at the beginning, a function was not redefined but it was executed in it.
In the second way, a = function () The code a that has not been executed in the function has been redefined. So the redefinition here is valid
Supplement 1:
Copy the codeThe code is as follows:
function a(){
alert('old');
}
var b=a;
function a(){
b();
alert('new');
}
Compilation time: First a is defined as alert("old"), then it is defined as b(); alert("new");
Runtime: b = function a(){b();alert("new");}, at this time, b is the same as a, and b is called directly in the function body. The result is the same whether it is called from a or from b, resulting in stack overflow.
on the other hand
Copy the codeThe code is as follows:
function a(){
alert('old');
}
var b=a;
var a=function(){
b();
alert('new');
}
Compilation time: a is defined as alert("old")
Runtime: b=function a(){alert("old")}; a=function(){b();alert("new")}; At this time, b's function body does not include any of ab, a only calls b... No stack overflow will occur regardless of whether it is...
Supplement 2:
Generally speaking, the first method is used to avoid code contamination, but if you need to keep the original function, you must use the second method. Anyway, both methods are in line with w3c.
In fact, the first writing method was only available later, and this writing method was optimized.