topical media & game development

talk show tell print

professional-javascript-04-PrototypeChainingExample.htm / htm



  <html>
  <head>
  <title>Example</title>
  </head>
  <body>
  <script type="text/javascript">
  function ClassA() {
  }
  
  ClassA.prototype.color = "red";
  ClassA.prototype.sayColor = function () {
      alert(this.color);
  };
  
  function ClassB() {
  }
  
  ClassB.prototype = new ClassA();
  
  ClassB.prototype.name = "";
  ClassB.prototype.sayName = function () {
      alert(this.name);
  };
  
  var objA = new ClassA();
  var objB = new ClassB();
  objA.color = "red";
  objB.color = "blue";
  objB.name = "Nicholas";
  objA.sayColor();
  objB.sayColor();
  objB.sayName();
  
  </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.