84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
`alert(sum(10,10));var sum=function(num1,num2){
return num1+num2;
};`Why is the error reported? Isn’t there a variable promotion?
When we write js code, we have two ways of writing, one is function expression, and the other is function declaration. What we need to focus on is:
Only function declaration forms can be promoted.
1. Function declaration form [Success]
function myTest(){ foo(); function foo(){ alert("我来自 foo"); } } myTest();
2. Function expression method [Failure]
function myTest(){ foo(); var foo =function foo(){ // 看这里 alert("我来自 foo"); } } myTest();
Read my article: http://www.jianshu.com/p/85a2...
Function expressions are not hoisted.
Read "Javascript Advanced Programming" again.
Declaration and expression are different. If you declare, not only the definition will be done in advance, but the assignment will also be done in advance, but the expression will not. For example:
a(); function a(){}; //等同于 var a = function(){}; a(); ///////对于表达式有 a(); var a = function(){}; //等同于 var a; a(); a = function(){}; //简单来讲就是表达式的赋值必须要等程序运行到相关行的时候才会进行
Same as above, your function creation method is in function literal form, change it to
alert(sum(10,10)); function sum(num1,num2){ return num1+num2; }
That’s it
When we write js code, we have two ways of writing, one is function expression, and the other is function declaration.
What we need to focus on is:
1. Function declaration form [Success]
2. Function expression method [Failure]
Read my article: http://www.jianshu.com/p/85a2...
Function expressions are not hoisted.
Read "Javascript Advanced Programming" again.
Declaration and expression are different. If you declare, not only the definition will be done in advance, but the assignment will also be done in advance, but the expression will not. For example:
Same as above, your function creation method is in function literal form, change it to
That’s it