jQuery jAM, Season 6

Exercise 6.1

A Vector Type

	 
function Vector(x,y){
	 this.x = x;
	 this.y = y;
	}
	 
Vector.prototype.plus = function(other){
	return new Vector(this.x + other.x, this.y + other.y)
	 };

Vector.prototype.minus = function(other){
	return new Vector(this.x - other.x,this.y- other.y);
};
Object.defineProperty(Vector.prototype, "length",
	 {
	 	get:function(){
	 	return Math.sqrt(this.x * this.x +this.y * this.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
	 
	

To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.