Skip to content

Instantly share code, notes, and snippets.

@TimBlock
Created January 19, 2016 11:09
Show Gist options
  • Save TimBlock/e60548ea0625c70c5902 to your computer and use it in GitHub Desktop.
Save TimBlock/e60548ea0625c70c5902 to your computer and use it in GitHub Desktop.
// Your code here.
function Vector(x,y){
this.x=x;
this.y=y;
};
Vector.prototype.plus = function(vector){
return new Vector(this.x+vector.x, this.y+vector.y)
}
Vector.prototype.minus = function(vector){
return new Vector(this.x-vector.x, this.y - vector.y)
}
Object.defineProperty(Vector.prototype, "length",{get: function(){
var x = Math.pow(this.x, 2);
var y = Math.pow(this.y, 2);
return Math.sqrt(x+y);
}
});
console.log(new Vector(1, 2).plus(new Vector(2, 3)));
// → Vector{x: 3, y: 5}
console.log(new Vector(1, 2).minus(new Vector(2, 3)));
// → Vector{x: -1, y: -1}
console.log(new Vector(3, 4).length);
// → 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment