35

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);
    //...
}

2 Answers 2

57

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Brilliant! This has illuded me since ancient time, yet it was so simple.. faceslap
@Lars-Erik To be honest, it's a mess :-). The upcoming edition of JavaScript (ECMAScript 6) will bring class syntax, which will make this pattern much simpler.
Then we're just left with the 10 year waiting period before all browsers with pre v6 are dead. :) I still like this way of "deriving classes" better than the various extend functions etc. (for now at least)
@Lars-Erik Feel free to start using ES6 classes today with a ES6-to-Es5 transpliler. You can transpile in-browser, or before with e.g. a Grunt task npmjs.org/package/grunt-traceur
still I don't understand how come it works - why is the DynamicBody an instanceof PhysicsBody? they both inherit from object but they do not inherit from each other! (assuming that PhysicsBody.prototype points to a generic Object...)
|
10

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.