function Parent(){
this.name = "parent";
console.log("this gets executed");
}
function Child(){
Parent.call(this) // doesnt the this here belongs to the child object?
}
var o = new Child();
为什么子对象中的this
调用父构造函数?this
不是指子对象吗?请指点一下菜鸟javascripter,谢谢!
发布于 2017-01-11 02:32:48
发布于 2017-01-11 02:33:03
this
只引用子对象。
在这里,您将在Parent
(即子对象)上下文中调用this
函数。
Parent.call(this)
类似于Parent()
,唯一的区别是调用方法的上下文。
https://stackoverflow.com/questions/41588279
复制相似问题