美文网首页
JavaScript Hoisting

JavaScript Hoisting

作者: 老马的春天 | 来源:发表于2017-06-01 17:30 被阅读16次

    Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

    function number() {
        return 1;
    }
    
    (function() {
        try {
            number();
        } catch (ex) {
            console.log(ex);
        }
        var number = function number() {
            return 2;
        };
    
        console.log(number());
    })();
    
    console.log(number());
    

    js会把声明提升到当前作用域的最上边,包括变量和函数声明。

    function number() {
        return 1;
    }
    
    (function() {
        console.log(number());
    
        function number() {
            return 2;
        }
    })();
    
    console.log(number());
    

    相关文章

      网友评论

          本文标题:JavaScript Hoisting

          本文链接:https://www.haomeiwen.com/subject/lmmefxtx.html