//Let´s create a new Type
function Point(x, y)
{
this.X = x;
this.Y = y;
}
//Now you can use this type
var myPoint new Point(10, 20);
var xPosition = myPoint.X;
var yPosition = myPoint.Y;
//We can use this type in other types
//Lets use the "Point" type in a "Line" type
function Line(startPoint, endPoint)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
this.CalculateLineLength = function() {
//a² + b² = c²
var aSquared = (this.EndPoint.X - this.StartPoint.X) * (this.EndPoint.X - this.StartPoint.X);
var bSquared = (this.EndPoint.Y - this.StartPoint.Y) * (this.EndPoint.Y - this.StartPoint.Y);
return Math.sqrt(aSquared + bSquared);
}
}
//We need two points
var myStartPoint = new Point(10, 20);
var myEndPoint = new Point(30, 40);
//Now we can create a "Line"
var myLine = new Line(myStartPoint, myEndPoint);
var lengthOfLine = myLine.CalculateLineLength();