以下代码执行后,num 的值是?var foo = function(x, y){
return x - y;
}
function foo(x, y){
return x + y;
}
var num = foo(1, 2);
var foo = function(x, y){ return x - y; } function foo(x, y){ return x + y; } var num = foo(1, 2);
//variable hoisting变量提升 var foo;//foo#1 var num; //function declaration hoisting函数声明提升 function foo(x, y){//foo#2 return x + y; } //function expression NOT hoisted函数表达式不会被提升 foo = function(x, y){//foo#3 return x - y; } num = foo(1, 2);//这里使用foo#3
refs:
1. https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function
4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function
5. https://stackoverflow.com/questions/40675821/what-happens-when-javascript-variable-name-and-function-name-is-the-same