Implement JavaScript inheritance, via Object.create vs new - Javascript Object

Javascript examples for Object:Constructor

Description

Implement JavaScript inheritance, via Object.create vs new

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*from   ww  w .j  av a 2 s .  co m*/
function my(){ this.publicProperty='SomeValue'; }
my.prototype = {
    doThis : function(){},
    doThat : function(){}
}
function MyClass(){}

MyClass.prototype = Object.create(my.prototype);

var obj=new MyClass();
console.log(obj.publicProperty);
console.log(obj);
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials