Answer the question
In order to leave comments, you need to log in
Inheritance in JS How to call a parent method through the inheritance chain?
It has its own implementation of the system for creating and inheriting "classes".
There is a possibility of inheritance in the form:
childClass.prototype = Object.create( parentClass.prototype );
childClass.prototype.constructor = childClass;
// Копируем методы родителя с добавлением префикса, или в спец. список
// Прописываем сверху методы наследника
childClass.prototype.parent = parentClass;
childClass.prototype.parentMethodList = parentMethodList;
childClass.prototype.childMethodList = childMethodList;
childClass.prototype.inheritedMethodList = inheritedMethodList;
...
constructor: function()
{
...
this.parent();
...
}
childClass.prototype.parent = function( ) { new parentClass.apply( parentClass, arguments ); };
Answer the question
In order to leave comments, you need to log in
Why not use the ready-made implementation? For example Backbone
function Class() {}
// Helper function to correctly set up the prototype chain for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
Class.extend = function(protoProps, staticProps) {
var parent = this, child, prop;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent constructor.
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor;
} else {
child = function() { return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
for (prop in parent) child[prop] = parent[prop];
for (prop in staticProps) child[prop] = staticProps[prop];
// Set the prototype chain to inherit from `parent`, without calling
// `parent` constructor function.
var Surrogate = function() { this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
for (prop in protoProps) child.prototype[prop] = protoProps[prop];
// Save parent prototype
child.prototype.__super__ = parent.prototype;
return child;
};
var Model = Class.extend({
constructor: function() {
console.log("Model:constructor()");
}
});
var User = Model.extend({
constructor: function() {
this.__super__.constructor();
console.log("User:constructor()");
}
});
var SuperUser = User.extend({
constructor: function() {
this.__super__.constructor();
console.log("SuperUser:constructor()");
}
});
var supeUser = new SuperUser();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question