Class factory example : Constructor « Object Oriented « JavaScript Tutorial






<html>
<head>
<title>Factory Example</title>
</head>
<body>
<script type="text/javascript">
function createCar(sColor, iDoors, iMpg) {
    var oTempCar = new Object;
    oTempCar.color = sColor;
    oTempCar.doors = iDoors;
    oTempCar.mpg = iMpg;

    oTempCar.showColor = function () {
        alert(this.color)
    };
    return oTempCar;
}

var oCar1 = createCar("red", 4, 23);
var oCar2 = createCar("blue", 3, 25);
oCar1.showColor();
oCar2.showColor();


</script>

</body>
</html>








25.3.Constructor
25.3.1.Constructor Paradigm
25.3.2.Object constructor
25.3.3.Constructor Example
25.3.4.Constructor prototype
25.3.5.Use Form input data to construct an object
25.3.6.Class factory example