JS Prototype

To make this easier to understand, lets change the word “Prototype” to “Template” for one second, and remember that almost everything in JS is an Object, this instance of the object get its characteristics, and abilities from a Global template that specify characteristics but not values,

the abilities are the functions or Methods, but what happen when you want an object to execute an ability that is not ingrain-coded in its template or prototype, well one thing is that we can create a new function that will do that and coded on its template (prototype), or if you want all instances of this specific Object to have this ability then you code it in the “Global Prototype of the Object”

  • Every JavaScript object has a prototype. The prototype is also an object.
  • All JavaScript objects inherit their properties and methods from their prototype

In the following example we want to create a new Method (ability) in the “Array-Object-Prototype” so all instances can use it:

Not all Object-Prototype are created equal, see this two variations.

A Function’s prototype: is the object instance, that will become the prototype for all objects created using this function as a constructor.

An Object’s prototype: is the object instance form which the object is inherited.

In the following example we console.log the prototype from a function and an object:

The prototype properties are borrow from its parent, so the instance property supersedes the prototype property. PS: LINK

 

 

in this case, means share an object that has: “Functions & Values” with another object that can use those “Functions and Values”. ┬ádoing so we don not need to define the same Functions over and over fore every object.

 

.



Copyright 2017. All rights reserved.

Posted January 23, 2017 by Edmundo in category "JS