Slide 16
Slide 16 text
RULE #8 (CONTINUED)
// immutable vector implementation
function Vec2d(x, y) {
this.x = x; this.y = y;
}
Vec2d.prototype.add = function(x, y) {
if (x instanceof Vec2d) {
y = x.y; x = x.x;
}
return new Vec2d(this.x + x, this.y + y);
};
var a = new Vec2d(1,2), b = new Vec2d(3,4);
for (var i = 0; i < 1e7; i++) a = a.add(b);