Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript Prototype

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 169
    Comment on it

    All the objects that are created in javascript has a prototype. In fact we can say that a prototype is an object itself. All javascript objects created inherits the properties and methods from their prototype. Primary use of prototype property is for inheritance. You are required to be familiar with the objects in javascript first before going through this blog.

     

    Prototype concepts in javascript:

     

    1. Every javascript function has a prototype property which is by default empty, and when we want to implement inheritance we assign properties and methods on this prototype property.
    2. Secondly, prototype attribute which points to the object it inherited its properties from. Generally it is referred as the prototype object, ans is set automatically when we create a new object.

     

    As we know there are two ways to create a javascript object , i.e, Object Literal and other by using the keyword “new”. The objects created with the object literals notation and with the object constructor inherits form the Object.prototype.

     

    var literalObject = new Object(); // the literalObject inherits from Object and as such its prototype attribute is Object.prototype
    
    
    var literalObject = {key: value} // shows the use of an object literal to create the literalObject; the literalObject inherits from object, thus its prototype attribute is Object.prototype.

     

     

    When objects are created with the help of “new” keyword, they get their prototype from the constructor function. For instance,

     

    function demo() {
    
    
    }
    
    
    var literalObject = new demo() // literalObject initialized with the demo() constructor.

     

    In brief, we can say that if an object is created with an object literal ( var newObject = {} ), it will inherit properties from the Object.prototype and we say its prototype attribute is Object.prototype. And if an object is created from a constructor function than it will inherit properties from that constructor. For instance, an object created with new Array() will have Array.prototype as its prototype.

     

    When is prototype used?

     

    1. Prototype-based Inheritance : as we know javascript does not have inheritance based on Classes, therefore all inheritance in javascript is made through prototype property.
    2. Accessing Properties on objects : when we want to access a property of an object, it is the object from where the search for the property starts.

     

    Happy Coding :)

 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: