首先,我们知道,构造函数(Constructor)中的this指向调用该构造函数所创建的实例对象(instance)。
但是,但我们在构造函数中定义一个隐形全局变量 that = this 的时候,并用该构造函数创建多个实例对象,那么这个变量that会指向哪一个实例对象呢?
function Constructor() {
that = this;
}
// console.log(that); // 报错:that is not defined
var instanceA = new Constructor();
console.log( that == instanceA ); // 结果为true
var instanceB = new Constructor();
console.log( that == instanceA ); // 结果为false
console.log( that == instanceB ); // 结果为true
由此可以看出,that将指向最后创建的实例对象。
最后
以上就是健壮香菇最近收集整理的关于构造函数中,this的指向的全部内容,更多相关构造函数中,this内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复