Prototype are link or pointer to other objects. All objects have prototype. Objects are chained together by the prototype chain.
//Constructor
function Office(name, location, domain)
{
this.name=name;
this.location=location;
this.domain=domain;
}
//Prototype
Office.Prototype={
interview: function(){
alert("Hi office location is"+ this.location);
}
};
var myoffice= new Office("expo-e", "london", "telecommunication");
myoffice.interview; // hi office location is london
In the above example
myoffice prototype is shared with Office
Office prototype is shared with object
Object prototype is shared with null
myoffice->Office->Object->null, makes the complete chain of prototype.
when you lookup a property on an object, it will walk up the prototype chain until its found. First my office is checked then the Office is checked, then object and finally it reaches null.
//Constructor
function Office(name, location, domain)
{
this.name=name;
this.location=location;
this.domain=domain;
}
//Prototype
Office.Prototype={
interview: function(){
alert("Hi office location is"+ this.location);
}
};
var myoffice= new Office("expo-e", "london", "telecommunication");
myoffice.interview; // hi office location is london
In the above example
myoffice prototype is shared with Office
Office prototype is shared with object
Object prototype is shared with null
myoffice->Office->Object->null, makes the complete chain of prototype.
when you lookup a property on an object, it will walk up the prototype chain until its found. First my office is checked then the Office is checked, then object and finally it reaches null.
Comments
Post a Comment