Saturday, September 6, 2014

Prototype

Prototype property of a function

Every function has a prototype property and it contains an object

>>> function foo(a, b){return a * b;}
>>> typeof foo.prototype
"object"

It's as if you added this property yourself like this:
>>> foo.prototype = {}

prototype property of a Constructor function

Let's take a look at the constructor function Gadget

function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function() {
return 'I am a ' + this.color + ' ' + this.name;
}
}

Adding methods and properties to the prototype property of the constructor function is another way to add functionality to the objects this constructor produces.

Adding properties to the prototype object

Let's add two more properties, price and rating, and a getInfo() method

Gadget.prototype.price = 100;
Gadget.prototype.rating = 3;
Gadget.prototype.getInfo = function() {
return 'Rating: ' + this.rating + ', price: ' + this.price;
};

another way to achieve the above result is to overwrite the prototype completely, replacing it with an object of your choice

Gadget.prototype = {
price : 100,
rating : 3,
getInfo : function() {
return 'Rating: ' + this.rating + ', price: ' + this.price;
}
};

How JavaScript Engine looks for a property

>>> var newtoy = new Gadget('webcam', 'black');
>>> newtoy.price;
100

JavaScript engine will look through all of the properties of the object.
If the property is not found, it will search the prototype of the constructor function used to create this object.

Gadget.prototype = {
price : 100,
rating : 3,
getInfo : function() {
return 'Rating: ' + Gadget.prototype.rating + ', price: ' + Gadget.prototype.price;
}
};

>>> newtoy.rating
3

>>> newtoy.constructor
Gadget(name, color)

>>> newtoy.constructor.prototype.rating
3

Overwriting Prototype's Property with Own Property

The own property takes precedence over the prototype's

Enumerating Properties

var a = [ 1, 2, 3 ];
for ( var i in a) {
console.log(a[i]);
}

var o = {p1: 1, p2: 2};
for (var i in o) {
console.log(i + '=' + o[i]);
}

o/p:
p1=1
p2=2

Points to note

Not all properties show up in a for-in loop. The properties that do show up are called enumerable.
Prototypes that come through the prototype chain will also show up as long as they are enumerable.

Related object methods

propertyIsEnumerable()

  • can check which properties are enumerable
  • return false for all of the prototype's properties

hasOwnProperty()

  • can check if a property is an own property versus prototype's

isPrototypeOf()

Every object also gets the isPrototypeOf() method
This method tells you whether that specific object is used as a prototype of another object.

var monkey = {
hair : true,
feeds : 'bananas',
breathes : 'air'
};

function Human(name) {
this.name = name;
}
Human.prototype = monkey;

>>> var george = new Human('George');

"Is monkey george's prototype?"

>>> monkey.isPrototypeOf(george)
true

The Secret __proto__ Link

The prototype property will be consulted when you try to access a property that does not exist in the current object.

var monkey = {
feeds : 'bananas',
breathes : 'air'
};
function Human() {
}
Human.prototype = monkey;

var developer = new Human();
developer.feeds = 'pizza';
developer.hacks = 'JavaScript';

>>> developer.constructor = 'junk'
"junk"

>>> typeof developer.constructor.prototype
undefined

but the following still works

>>> developer.breathes
"air"

This shows that the secret link to the prototype still exists __proto__

>>> developer.__proto__
Object feeds=bananas breathes=air

because it does not exist in Internet Explorer, so your scripts won't be portable.

__proto__ is a property of the instances, whereas prototype is a property of the constructor functions

Augmenting Built-in Objects

Array, String, and even Object, and Function can be augmented through their prototypes.
Can, for example, add new methods to the Array prototype and in this way make them available to all arrays.

String.prototype.reverse = function() {
return Array.prototype.reverse.apply(this.split('')).join('');
}

>>> "Stoyan".reverse();
"nayotS"

No comments:

Post a Comment