JavaScript-变量提升和函数提升

2017-01-11  本文已影响17人  stevekeol

变量提升: 

只是提升变量的声明,并不会把赋值也提升上来。

var name ="Outer";

(function(){

        console.log(" name was "+ name); // Outputs: "name was undefined"

        var name ="Inner";

        console.log("name is "+ name); // Outputs: "name is Inner"

})();

函数提升:

不仅提升函数名,也提升了真正的函数定义;

但函数定义提升仅仅作用于函数定义,而不是函数表达式。

a(); // Outputs: "Definition hoisted!"

b(); // TypeError: undefined is not a function

function a() {console.log("Definition hoisted!"); }

var b =function() {console.log("Definition not hoisted!"); };


彩蛋-结果验证(chrome)

步骤 :<F12>键,在console中粘贴相应的代码段,回车(见下图)。

chrome中结果验证

TIPS:本文只是个人对过去的总结,不保证简洁性、系统性、易懂性。如需交流:zhejiangdaxue2011(微信号)

上一篇 下一篇

猜你喜欢

热点阅读