function haha(){
this.one=function(){
console.log(this)
}
this.two=function(){
this.one();
console.log(this)
}
}
var hehe = new haha()
hehe.two()
通过对象的属性调用,this不是应该指向这个对象的吗(hehe对象)?
为什么这里会输出两个haha对象呢.
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为hehe是Haha的一个实例名呀。你也可以叫做heihei。。。对于Haha类本身来说它是被调用者,是不感知你给他取什么实例名的。
从上面的代码可以看出
console.log(hehe)输出的是Haha。实例化Haha返回的就是一个Haha对象,只是赋值给了hehe而已