Skip to content

Instantly share code, notes, and snippets.

@dgellow
Last active August 29, 2015 13:57
Show Gist options
  • Save dgellow/9665400 to your computer and use it in GitHub Desktop.
Save dgellow/9665400 to your computer and use it in GitHub Desktop.
function Point(x, y, proto) {
var p = Object.create(proto || Point.prototype);
p.x = x;
p.y = y;
return p;
};
Point.prototype = {
plus: function(point) {
this.x += point.x;
this.y += point.y;
return this;
},
minus: function(point) {
this.x -= point.x;
this.y -= point.y;
return this;
},
get distance() {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
}
};
console.log(Point(1, 2).plus(Point(2, 3)));
// → Point{x: 3, y: 5}
console.log(Point(1, 2).minus(Point(2, 3)));
// → Point{x: -1, y: -1}
console.log(Point(3, 4).distance);
// → 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment