我知道这个问题以前问过很多次,但我仍然无法理解它。我正在使用 javascrit oop,我需要从子类函数 this.fullArr
调用父类函数 this.returnResult
。
function parantCls(){
this.sCus = [];
this.aCus = [];
this.response;
this.returnResult = function(msg){
this.response = {
result : msg
};
return this;
}
}
function resonse(){
parantCls.apply(this, arguments);
this.fullArr = function(){
// call parent function
parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus);
return this.response;
}
}
resonse.prototype = new parantCls();
为什么 parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus);
不起作用。我也像这样使用了call and apply
parantCls.prototype.returnResult.call(this,'设置customField应该是数组而不是' + typeof this.sCus);
但还是不行。有什么问题
最佳答案
如果您使用原型(prototype)链正确继承。
这应该有效
this.returnResult('setting customField should be array not ' + typeof this.sCus)
顺便说一下,你的继承看起来有问题。使用这种格式。
var Subclass = function() {
Superclass.call(this);
};
Subclass.prototype = Object.create(Superclass.prototype);
Subclass.prototype.constructor = Subclass;
Subclass.prototype.someMethod = function (value) {
this.x = value;
};
关于javascript - oop 从子类调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909479/