声明提前

    1. var 声明变量,变量名声明提前,值留在原处,
    2. function关键字声明函数,整段函数声明提前,
    3. var变量名与function函数名相同的情况,先声明function函数,在声明var
    1. var test = function(num){
    2. console.log(num);
    3. var num =20;
    4. function num(){
    5. console.log("hello world")
    6. }
    7. }
    8. test(10);

    在一个作用域中函数名和变量名相同情况下,函数会覆盖变量

    1. console.log(num);
    2. var num =10;
    3. function num(){
    4. console.log("hello world")
    5. }

    在function声明变量时,声明提前,相当于放置顶部

    1. <script>
    2. // function num(){
    3. // console.log("hello world")
    4. // }
    5. console.log(num);
    6. var num = 10;
    7. function num(){
    8. console.log("hello world")
    9. }
    10. console.log(num);

    示例

    1. <script>
    2. function num(a){
    3. /*
    4. var a= function(){
    5. console.log("hello world")
    6. }
    7. */
    8. console.log(a);
    9. var a=10;
    10. function a(){
    11. console.log("hello world")
    12. }
    13. console.log(a);
    14. }
    15. num(20);
    16. </script>