javaScript prototypes

javaScript is a prototype-based object-oriented language.

the every object has a prototype property that gives it its structure .the prototype is a reference to an object describing the code and data that all objects of that type have in common.a very important aspect of the prototype is that it is shared.

the object.prototype property allows you to add properties and methods to an Object

syntax:

          object.prototype.property_name  =  value 

the prototype is a property that gets created as soon as you define the function.its initial value is an empty object


adding method and property using the prototype

the constructor with new operator can be used to create a new object.

in this example we use the prototype property to add properties and method to an object:

function    user(first_name,  last_name){
   
               this.firstname  = first_name ;// this keyword  represent the current object
           
               this.lastname  = last_name ;
       }
       
       
var   sandeep  =  new   user('sandeep kumar', 'nehra') ;
      
        user.prototype.age = 25 ;
              
        user.prototype.salary = 10000 ;
              
        user.prototype.message = function(){
         
                           alert("this is sandeep kumar, i am software developer"); } 
 
  
     document.write('<br/>'+"the firstname ="+sandeep.firstname);
     
     document.write('<br/>'+"the lastname   ="+sandeep.lastname);
     
     document.write('<br/>'+"the age    "+sandeep.age);
     
     document.write('<br/>'+"the salary    "+sandeep.salary);
     
                                             sandeep.message();   

the another way to add properties and methods to an object

function    user(first_name,  last_name){
   
               this.firstname  = first_name ;// this keyword  represent the current object
           
               this.lastname  = last_name ;
       }
       
       
var   sandeep  =  new   user('javascript', 'prototype') ;

                  user.prototype{
                  
                                        age    : 25,  // add prototype properties to an object
                                                             
                                        salary : 100000,
                                                             
message: function(){ alert("this is the javascript prototype,  who i am") ;}
                                                             
                                   }   
                                        
                                              //access  age prototype property
              document.write("the user age ="+sandeep.age);                              

overwriting prototype's property withOwn property

what if the object does have its own property and the prototype also has one with the same name?

the own property takes precedence over the prototype's.

function    user(first_name,  last_name){
   
               this.firstname  = first_name ;// this keyword  represent the current object
           
               this.lastname  = last_name ;
       }
       
       
var   sandeep  =  new   user('sandeep kumar', 'nehra') ;
      
        user.prototype.firstname = 'ram' ;
              
        user.prototype.lastname =  'kumar' ;
        
        user.prototype.age  = 45 ;        
              
  
 
     document.write('<br/>'+"the firstname ="+sandeep.firstname);
     
     document.write('<br/>'+"the lastname   ="+sandeep.lastname);
     
     document.write('<br/>'+"the age    "+sandeep.age);
  

note: a very important aspect of the prototype is that it is shared that is, there is only one copy of the prototype that all objects created with the same constructor use.


object constructor property

the object instance are created with constructor, which are special function that prepare new instance of an object for use.

the constructor property is a member of the prototype of every object that has a prototype. the constructor property contains a reference to the function that constructs instances of that particular object.

                var   bool_type  = new  Boolean(true) ;
                
                var   str_type    = new  String("ram") ;
                
                var   date_type  = new  Date() ;
                
 if(bool_type.constructor == Boolean) {

                            document.write("this is boolean  object"+"<br/>") ; }

 if(str_type.constructor == string){
                
                            document.write("this is  string  object"+"<br/>") ; }

 if(date_type.constructor == date){

                            document.write("this is  date   object") ; }