topical media & game development

talk show tell print

professional-javascript-03-FactoryExample.htm / htm



  <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();    //outputs "red"
  oCar2.showColor();    //outputs "blue"
  
  </script>
   
  </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.