1. Factory:
function showColor(color) {
alert(this.color);
}
function createCar(color, doors, mpg) {
var a = new Object();
a.color = color;
a.doors = doors;
a.mpg = mpg;
a.showColor = showColor;
return a;
}
2.Constructor:
function Car(door, color, mpg) {
this.door = door;
this.color = color;
this.mpg = mpg;
this.showColor = function () {
alert(this.color);
}
}
var oCar1 = new Car("red", 4, 23);
var oCar2 = new Car("blue", 3, 25);
oCar1.showColor(); //outputs "red"
oCar2.showColor(); //outputs "blue"