Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit parent constructor arguments

I'm browsing the discussion for a similar topic, but can't find my situation...

Am trying call parent constructors with parameters... can't seem to get it right.

I have a PhysicsBody superclass that takes aNode as its only constructor argument:

function PhysicsBody(aNode) {     this.userData = aNode;     // ... } 

Of this PhysicsBody inherits a DynamicBody class. Is constructor also takes aNode as only argument... Like I would do it in Java, I'd love to call something equivalent to "super(aNode"); Can't seem to find out how.

Here's the DynamicBody class:

// Wanted to give "new PhysicsBody(this, aNode)", but that fails! DynamicBody.prototype = new PhysicsBody(); DynamicBody.prototype.constructor=DynamicBody;  function DynamicBody(aNode) {      // calling the parent constructor fails too:     // PhysicsBody.prototype.constructor.call(this, aNode);     //... } 
like image 590
Jem Avatar asked Oct 16 '11 17:10

Jem


People also ask

Can we inherit constructor from parent class?

Constructors are not members of classes and only members are inherited. You cannot inherit a constructor.

Does inheritance inherit constructors?

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Can constructor be inherited through a subclass that calls the superclass constructor?

Constructors are not inherited. The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters to set the private instance variables of the superclass.

Does a child constructor always invoke a parent constructor?

No. The first thing a subclass constructor must do is to invoke one of the superclass constructors. If you don't, then the compiler calls the no-arg constructor of the superclass for you.


2 Answers

One way to do it:

function PhysicsBody( aNode ) {     this.userData = aNode; }  PhysicsBody.prototype.pbMethod = function () {};  function DynamicBody( aNode ) {     PhysicsBody.call( this, aNode ); }  // setting up the inheritance DynamicBody.prototype = Object.create( PhysicsBody.prototype );  DynamicBody.prototype.dbMethod = function () {}; 

Now, when you do

var pb = new PhysicsBody( '...' ); 

the instance pb gets a userData property and also inherits the methods from PhysicsBody.prototype (pbMethod in this case).


When you do

var db = new DynamicBody( '...' ); 

the instance db gets a userData property and also inherits the methods from DynamicBody.prototype (dbMethod in this case), which in turn inherits from PhysicsBody.prototype.

like image 114
Šime Vidas Avatar answered Oct 15 '22 05:10

Šime Vidas


If I understand you correctly, by saying you want to inherit the parent constructor arguments, you mean that new DynamicBody(1, 2, 3) will internally call PhysicsBody(1, 2, 3) for the DynamicBody instance.

This can be accomplished by using .apply and passing arguments along: http://jsfiddle.net/pmkrQ/.

function DynamicBody() {     PhysicsBody.apply(this, arguments); } 
like image 37
pimvdb Avatar answered Oct 15 '22 05:10

pimvdb