With the "old way" of creating classes in JavaScript, you mostly avoid doing something like this:
function Car {
this.make = "Renault";
this.model = "Twingo";
this.name = function () {
return this.make + this.model;
};
}
because it would create a new function object at each instantiation of the class, so you'd rather do this:
function Car {
this.make = "Renault";
this.model = "Twingo";
}
Car.prototype.name = function() {
return this.make + this.model;
}
In ES6, and with the newer class syntax we can do something like:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
name () {
return this.make + this.model;
}
}
Does this only create one shared function object? Or does it instance a new one at each new call like the first one?