Javascript 从子原型调用父原型
我的家长班是Javascript 从子原型调用父原型,javascript,inheritance,Javascript,Inheritance,我的家长班是 function Parent() {} Parent.prototype.get = function() {} Parent.prototype.start= function() { this._start() } 我的孩子是 function Child(){ Parent.call(this, arguments) } Child.prototype._start = function(){ this.get() /* erro
function Parent() {}
Parent.prototype.get = function() {}
Parent.prototype.start= function() { this._start() }
我的孩子是
function Child(){ Parent.call(this, arguments) }
Child.prototype._start = function(){ this.get() /* error here - this.get is not a function*/ }
util.inherits(Child, Parent);
当我这样做的时候
new Child().start()
我得到一个错误
这个。get不是一个函数
。如何调用父原型函数?谢谢。由于不鼓励使用util.inherits
,您应该使用for类,但您似乎只有常规函数,这意味着您可以在开始进一步扩展之前将子对象的原型设置为与父对象相同
函数父函数(){}
Parent.prototype.get=函数(){
console.log('worksfine');
}
Parent.prototype.start=函数(){
这个;
}
函数子(){
调用(这个,参数);
}
Child.prototype=Parent.prototype;
prototype.\u start=function(){
这个。get();
}
var instance=new Child();
instance.start()
console.log(this)
要查看此引用的内容,请考虑使用ES6类和super
方法。