
var a = 5;
function test(){
a = 0;
console.log(a);
console.log(this.a);
var a;
console.log(a);
}
new test();


new test()时候,test内部会创建一个空对象,var this = {}, 最后return this;所有this指向{},此时a为undefined;
本文通过一个具体的JavaScript函数实例,解析了在使用new关键字调用函数时,函数内部this的指向问题。具体分析了变量声明、函数调用及this在不同上下文中的表现。

var a = 5;
function test(){
a = 0;
console.log(a);
console.log(this.a);
var a;
console.log(a);
}
new test();


new test()时候,test内部会创建一个空对象,var this = {}, 最后return this;所有this指向{},此时a为undefined;
561

被折叠的 条评论
为什么被折叠?