javastript的变量提升(hoisting)

前端之家收集整理的这篇文章主要介绍了javastript的变量提升(hoisting)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

容易混淆的概念(Easily confused concepts)

  1. 声明会提升,但赋值语句不会。

    var test = 1;
    

    function one(){
    console.log(test) //undefined,Throws a ReferenceError in strict mode
    var test = 2;
    }

    one();

    提升后:

    var test = 1;
    

    function one(){
    var test;
    console.log(test); //undefined,Throws a ReferenceError in strict mode
    test = 2;
    }

    one();

  2. 提升优先级

    var one = 1;
    

    function one(){

    }

    console.log(typeof(one)); //number,而不是function

    提升后:(函数声明会提升)

    function one(){
    

    }

    var one = 1;

    console.log(typeof(one)); //number,而不是function

猜你在找的程序笔记相关文章