Eloquent JavaScript: The Secret Life of Objects
There are a few exercises at the end of the chapter.
What the difference between following solutions?
By author:
class Vec {
constructor(x, y) {
this.x = x;
this.y = y;
}
plus(other) {
return new Vec(this.x + other.x, this.y + other.y);
}
minus(other) {
return new Vec(this.x - other.x, this.y - other.y);
}
}
My solution:
class Vec {
constructor(x, y) {
this.x = x;
this.y = y;
}
plus(vec) {
this.x += vec.x;
this.y += vec.y;
return this;
}
minus(vec) {
this.x -= vec.x;
this.y -= vec.y;
return this;
}
}