如何将参数传递给继承的方法?
interactiveElement.prototype.moveTowards = function(target){
console.log(target);
}
projectile.prototype.fire = function(target){
interactiveElement.prototype.moveTowards.call(this); //how do I pass target
}
收到错误:未捕获的TypeError:无法读取未定义的属性'0'
如果必须使用call
,只需在“ this”参数后面传递参数即可:
interactiveElement.prototype.moveTowards.call(this, target); //how do I pass target
但是,如果它实际上是一个继承的方法,则可以简化它:
this.moveTowards(target);
如果原型链moveTowards
中projectile
的任何位置,都可以通过进行访问,this
而不是使用...prototype...call
。当你有原型的继承,你通常只使用call
到引用的是“由成员重载链上的低,”一个功能例如,如果projectile
有一个moveTowards
和interactiveElement
有moveTowards
,你要拨打ineractiveElement
的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句