Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Prototypical inheritance in javascript

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 155
    Comment on it

    prototypical inheritance... We have used many different types of inhertance which are based on classes but today we are going to discuss the prototypical inheritance in javascript. It is based on objects, you can simply inherit one object to other object.

    Suppose we have two objects

    var countries = {'India':1,'America':2};
    var states = {'Delhi':1,'Washington':2};
    

    lets inherit the states object to countries object with the help of special property _proto_

    states.__proto__ = countries;
    

    When you access the states then interpreter will find it in countries object. Please have a look an another example.

    var animal = {
      eat: function() { 
        alert( "I'm ok" )
        this.full = true
      }
    }
    
    var rabbit = { 
      jump: function() { /* something */ }
    }
    
    rabbit.__;proto__ = animal  
    
    rabbit.eat();
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: