Class factory example : Constructor : Object Oriented JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Object Oriented » Constructor »

 

Class factory example

















<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"423);
var oCar2 = createCar("blue"325);
oCar1.showColor();
oCar2.showColor();


</script>

</body>
</html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Object Oriented
» Constructor