Node.js examples for Data Structure:Vector
Vector, plus and minus
function Vector(x,y) { this.x = x;/*ww w . ja v a2s . c o m*/ this.y = y; } Vector.prototype.plus = function(vector) { xSum = this.x + vector.x; ySum = this.y + vector.y; return new Vector(xSum,ySum); } Vector.prototype.minus = function(vector) { xDiff = this.x - vector.x; yDiff = this.y - vector.y; return new Vector(xDiff,yDiff); } Object.defineProperty(Vector.prototype, "length", { get: function() { return Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2)) } }) console.log(new Vector(1, 2).plus(new Vector(2, 3))); console.log(new Vector(1, 2).minus(new Vector(2, 3))); console.log(new Vector(3, 4).length);