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?