Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Prototype property is created when a function is declared. </p> <p>For instance:</p> <pre><code> function Person(dob){ this.dob = dob }; </code></pre> <p>Person.prototype property is created internally once you declare above function. Many properties can be added to the Person.prototype which are shared by Person instances created using new Person().</p> <pre><code>// adds a new method age to the Person.prototype Object. Person.prototype.age = function(){return date-dob}; </code></pre> <p>It is worth noting that <code>Person.prototype</code> is an <code>Object</code> literal by default (it can be changed as required). </p> <p>Every instance created using new Person() has a <code>__proto__</code> property which points to the Person.prototype. This is the chain that is used to traverse to find a property of a particular object. </p> <pre><code>var person1 = new Person(somedate); var person2 = new Person(somedate); </code></pre> <p>creates 2 instances of Person, these 2 objects can call age property of Person.prototype as person1.age, person2.age.</p> <p>In the above picture you can see that Foo is a Function Object and therefore it has a <code>__proto__</code> link to the Function.prototype which in turn is an instance of Object and has a <code>__proto__</code> link to Object.prototype. The proto link ends here with <code>__proto__</code> in the Object.prototype pointing to null.</p> <p>Any object can have access to all the properties in its proto chain as linked by <code>__proto__</code> , thus forming the basis for prototypal inheritance.</p> <p><code>__proto__</code> is not a standard way of accessing the prototype chain, the standard but similar approach is to use Object.getPrototypeOf(obj).</p> <p>Below code for <code>instanceof</code> operator gives a better understanding:</p> <p>object <code>instanceof</code> Class operator returns <code>true</code> when an object is an instance of a Class, more specifically if <code>Class.prototype</code> is found in the proto chain of that object then the object is an instance of that Class.</p> <pre><code>function instanceOf(Func){ var obj = this; while(obj !== null){ if(Object.getPrototypeOf(obj) === Func.prototype) return true; obj = Object.getPrototypeOf(obj); } return false; } </code></pre> <p>The above method can be called as : <code>instanceOf.call(object,Class)</code> which return true if object is instance of Class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload