调用继承方法 javascript 个原型
calling inherited methods javascript prototypes
我有一个扩展 "class" 使用原型扩展基础 "class"。我遇到的问题是如何从继承 classes 原型方法调用在基 class(基本方法)的原型上定义的方法。
function Obj() {
this.name;
}
Obj.prototype.baseMethod = function(usefulstuff) {
alert(usefulstuff);
}
function extendedObj() {
Obj.call(this);
}
extendedObj.prototype = new Obj();
extendedObj.prototype.constructor = extendedObj;
extendedObj.prototype.anotherMethod = function() {
this.baseMethod(stuff);//gives this.baseMethod is not a function and direct call gives baseMethod is not defined
}
var a = new extendedObj();
a.anotherMethod();
肯定是因为两个对象的原型是相同的并且只是添加了方法,并且因为原型方法是 public,所以这应该没问题,除非这不是原型链接的工作方式?
你可以加个_super
属性指代超class。有关更多详细信息,请参见此处。 http://ejohn.org/blog/simple-javascript-inheritance/
另一种方法是使用Superclass.prototype.desiredMethod.call(this, args...)
直接从超级class的原型调用方法。有关更多详细信息,请参见此处。 http://blog.salsify.com/engineering/super-methods-in-javascript
我有一个扩展 "class" 使用原型扩展基础 "class"。我遇到的问题是如何从继承 classes 原型方法调用在基 class(基本方法)的原型上定义的方法。
function Obj() {
this.name;
}
Obj.prototype.baseMethod = function(usefulstuff) {
alert(usefulstuff);
}
function extendedObj() {
Obj.call(this);
}
extendedObj.prototype = new Obj();
extendedObj.prototype.constructor = extendedObj;
extendedObj.prototype.anotherMethod = function() {
this.baseMethod(stuff);//gives this.baseMethod is not a function and direct call gives baseMethod is not defined
}
var a = new extendedObj();
a.anotherMethod();
肯定是因为两个对象的原型是相同的并且只是添加了方法,并且因为原型方法是 public,所以这应该没问题,除非这不是原型链接的工作方式?
你可以加个_super
属性指代超class。有关更多详细信息,请参见此处。 http://ejohn.org/blog/simple-javascript-inheritance/
另一种方法是使用Superclass.prototype.desiredMethod.call(this, args...)
直接从超级class的原型调用方法。有关更多详细信息,请参见此处。 http://blog.salsify.com/engineering/super-methods-in-javascript