-1

I have a class that needs a reference to a property from it's parent. I would like to know if it is better to pass in the property to the constructor of the child class and set it as a new property of the child class, or while in the parent class add it as a prototype of the child class.

implemenatation A:

// parent-class.js

let childClass = require('childClass.js');

class foo {

    constructor() {
        this.initChild = new childClass(this.superCoolMethod);
    }

    superCoolMethod() {
         return 'this method does super cool stuff and the child needs it';
    }

}

// child-class.js

class bar {
    constructor(superCoolMethod) {
        this.superCoolMethod = superCoolMethod;
    }

    callCoolMethod() {
        this.superCoolMethod();
    }

}

implemenatation B:

// parent-class.js

let childClass = require('childClass.js');

class foo {
    constructor() {
        this.childClass.prototype.superCoolMethod = superCoolMethod;
        this.initChild = new childClass(this.superCoolMethod);
    }

    superCoolMethod() {
         return 'this method does super cool stuff and the child needs it';
    }
}

// child-class.js

class bar {

    callCoolMethod() {
        this.superCoolMethod();
    }

}

Which implementation would be more performant and are there any better ways to achieve this?

trickpatty
  • 433
  • 7
  • 18
  • 2
    Take a look at http://wiki.c2.com/?PrematureOptimization – Adam Wolski Feb 02 '17 at 16:44
  • What's your goal is assigning a method from one instance to an object of another instance? (And beware using `super` in that case, it may not do what you expect.) – T.J. Crowder Feb 02 '17 at 16:51
  • my goal would be to have the child class be able to call a method from the parent class – trickpatty Feb 02 '17 at 16:52
  • Okay. Note that the way you're doing it, when it does, `this` will not be set correctly. I would tend to lean toward having a class that acts like an interface, and passing an instance of that into `child` rather than just a method. But in isolation it's hard to tell what design compromises to make. – T.J. Crowder Feb 02 '17 at 16:53
  • I'm trying to wrap my head around exactly what you mean by an interface... I know what interfaces are in terms of TypeScript but as far using that in this case I'm not totally sure how that would work @T.J.Crowder – trickpatty Feb 02 '17 at 17:05
  • 1
    Yeah, sorry, this is JavaScript, duck typing is sufficient. I'd pass the parent instance in rather than just its method, and then have the child call the method via that parent instance, so `this` is correct. – T.J. Crowder Feb 02 '17 at 17:10
  • ok, makes sense. thank you. @T.J.Crowder – trickpatty Feb 02 '17 at 17:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134699/discussion-between-trickpatty-and-t-j-crowder). – trickpatty Feb 02 '17 at 17:24

2 Answers2

1

Which implementation would be more performant...

It doesn't matter. It's a massively bad idea for class foo to reach out and change the prototype of class childClass. (But the instance property would be very-very-very-slightly faster, because the property lookup stops at the instance, rather than not finding it at the instance and then needing to look to the prototype. In the real world, the odds of it making any noticeable difference at all are near zero.)

Remember: That prototype is used by all other childClass instances, even those completely unrelated to foo code. The link between instance and its prototype is a link; instances don't get a copy of their prototype. So for example:

class Example {
};

const e = new Example();

console.log(e.foo);   // undefined

Example.prototype.foo = 42;

console.log(e.foo);   // 42

and are there any better ways to achieve this?

Set it as a property on the instance (e.g., implementation A or similar).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • when you say "Set it as a property on the instance." do you mean this? : `this.initChild.superCoolMethod = this.superCoolMethod;` – trickpatty Feb 02 '17 at 16:50
  • @trickpatty: I mean implementation A (or yeah, you could just assign it after constructing like that). But note the question on the question. :-) – T.J. Crowder Feb 02 '17 at 16:52
0

I would set it as a property. It is the way it makes sense. If you change the prototype, you will have the added method in all subsequent new childClass() objects. That may not be desired behavior.

Workion
  • 37
  • 4