Javascript oop从子类调用父函数
我知道这个问题以前问过很多次,但我还是无法理解。我使用的是javascrit oop,我需要调用子类函数this.fullArr中的父类函数this.returnResultJavascript oop从子类调用父函数,javascript,Javascript,我知道这个问题以前问过很多次,但我还是无法理解。我使用的是javascrit oop,我需要调用子类函数this.fullArr中的父类函数this.returnResult function parantCls(){ this.sCus = []; this.aCus = []; this.response; this.returnResult = function(msg){ this.response = { r
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(这个,'settingcustomfield应该是数组而不是'+typeof this.sCus)代码>不工作。im也像这样使用调用和应用
parantCls.prototype.returnResult.call(这个,'settingcustomfield应该是数组而不是'+typeof this.sCus)代码>
但还是不行。如果使用原型链正确继承,问题是什么
这应该行得通
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;
};
如果使用原型链正确继承
这应该行得通
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;
};
根据您调用fullArr
的方式,您最后的代码(parantCls.prototype.returnResult.call(this,…
)应该可以工作(尽管它不必要地复杂,因为您可以像gzc指出的那样只使用this.returnResponse
),因此:请使用可运行的堆栈片段更新您的问题([]
工具栏按钮)。根据您调用fullArr
的方式,您最后的代码(parantCls.prototype.returnResult.call(this,…
)应该可以工作(尽管它不必要地复杂,因为您可以像gzc指出的那样使用this.returnResponse
)。因此:请使用可运行的堆栈片段更新您的问题(使用[]
工具栏按钮)。“顺便说一句,您的继承看起来是错误的。”这没有错,因为他们没有使用任何原型函数。但是,是的,最好完全设置它。注意:您遗漏了上面重要的var
s,可能需要提及ES2015语法。(最后遗漏了一个;
)@t.J.Crowder您非常小心。-“顺便说一句,您的继承看起来是错误的。”这没有错,因为他们没有使用任何原型函数。但是,是的,最好完全设置它。注意:您遗漏了上面重要的var
s,可能需要提及ES2015语法。(最后遗漏了一个;
。@t.J.Crowder您非常小心。:-)