I have a Shape
class and a Circle
class that inherits from the Shape class.
Shape.js
function Shape(id, name, color) {
this.id = id;
this.name = name;
this.color = color;
Shape.prototype.getDisplayName = () => {
return this.id + ":" + this.name;
}
};
module.exports = Shape;
Circle.js
const Shape = require('./shape');
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;
function Circle(id, name, color, radius) {
Shape.call(this, id, name, color);
this.radius = radius;
};
module.exports = Circle;
This works fine, but when I have more than one Circle object, the getDisplayName
function is using the wrong context.
app.get('/', async (req, res) => {
let circle1 = new Circle('id1', 'Circle', 'red', 5);
let circle2 = new Circle('id3', 'Circle', 'blue', 12);
let shapes = [circle1, circle2];
for(let shape in shapes){
console.log(shapes[shape].getDisplayName());
}
});
This prints out
id3:Circle
id3:Circle
I found a SO answer that gave a bit of help. By adding this.getDisplayName = this.getDisplayName.bind(this)
to the Circle constructor, it works. However, I'd prefer not having to do this for every parent method on every child. Is there a better way?