is it possible to clone idiomatically a javascript class instance from within of its own method? Others languages like Java allow me to overwrite/clone the current instance by either construct overload or this
overwrite. Javascript does not seems to like it, and throws the error SyntaxError: Invalid left-hand side in assignment
. THere is an example below for this verbose explanation.
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
renew() {
this = new Car('Ferrari', 2021);
return this
}
}
car = new Car('Ford', 206)
car.renew()