Use prototype to add method to object - Javascript Object

Javascript examples for Object:prototype

Description

Use prototype to add method to object

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  w  ww .j  a va 2  s. c o m*/
function Obj(n){
    this.name = n;
}
Obj.prototype.disp = function(){
    console.log(this.name);
};
var l1=new Obj("one");
var l2=new Obj("two");
var lessons=[l1,l2];
lessons[0].disp();
console.log(lessons[0].name);
    }

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

Related Tutorials