<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>JavaScript: Inheritance</title>
  <style type="text/css">
  </style>
  <script type="text/javascript">
  <![CDATA[

    // Root
    function tRoot() {
      // undefined
      this.value = 1; // 1
    } 
    tRoot.prototype = { 
      method: function() { 
        this.value++; // 3
        this.virtualMethod(); 
      },
      virtualMethod: function() { 
        this.value++; // 5
      },
    }

    // Derived Class
    function tDerived() { 
      this.value++; // 2

      this.virtualMethod = function() { 
        this.value++; // 4
        this.__proto__.virtualMethod.call(this); // call inherited method
      };
    }
    tDerived.prototype = new tRoot; // 1

    // Test Function
    function test() {
      var object = new tDerived(); // 1
      object.method();
      if (object.value == 5) {
        document.getElementById('result').childNodes[0].data = 'PASS';
      } else {
        document.getElementById('result').childNodes[0].data = 'FAIL ('+object.value+')';
      }
    }

  ]]>
  </script>
 </head>
 <body onload="test()">
   <p id="result">FAIL</p>
 </body>
</html>
