Last active
August 29, 2015 13:57
-
-
Save dgellow/9665400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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