V
V
Vladimir B.2015-06-22 16:04:56
JavaScript
Vladimir B., 2015-06-22 16:04:56

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;

I implement inheritance: Class1 -> Class2 -> Class3.
When initializing Class3 , I initialize Class2 , and that, in turn , Class1 .
...
constructor: function()
{
...
this.parent();
...
}

But the problem is that at the moment of initialization of Class2 , the scope of this remains on Class3 , therefore, the subsequent call to this.parent calls the initialization of Class2 again instead of Class1 (just constant initialization of Class2 in recursion comes out) .
Tried to implement something like:
childClass.prototype.parent = function( ) { new parentClass.apply( parentClass, arguments ); };

But that's not it.
How to get out of this situation?
Thank you.
Listing of the version I'm picking: pastebin.com/iirPtLUJ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2015-06-22
@ange007

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 question

Ask a Question

731 491 924 answers to any question