Say suppose that we have following function declaration:
function Person()
{
this.surname = "zainal";
this.givenname = "aripin";
}
Person.prototype.city = "sidoarjo";
//instantiate object
var aPerson = new Person();
If we run this script in Mozilla and turn the Firebug on then we should be able to see all properties and their values of the inspected object. By inspecting the Person object we’ll find an interesting property:

What’s the __proto__ property anyway? The __proto__ here is used to explain the prototype chain; this property is not available in the language itself. In some browser (like Firefox) this property is representation of [[Prototype]] hidden property which is part of the ECMAScript specification.
The prototype gives us the flexibility to add behavior of new object. A prototype is an object and
every function we create automatically gets a prototype property that points to a new blank object. This object is almost identical to an object created with an object literal or Object() constructor, except that its constructor property points to the function we create and not to the built-in Object().
Since prototype is an object we can assign it to any kind of javascript object.
Person.prototype = {
firstname : "zainal",
lastname : "aripin",
show: function () {
alert(this.firstname + ' ' + this.lastname)
}
}
var bPerson = new Person();
bPerson.show(); // shows "zainal aripin"
What happens to the object if the prototype is assigned by new value after object instantiation? This object will still retain the original prototype behavior. Consider following example:
function Person()
{
this.surname = "zainal";
this.givenname = "aripin";
}
Person.prototype.city = "sidoarjo";
//instantiate object
var aPerson = new Person();
Person.prototype = {
firstname : "zainal",
lastname : "aripin",
show: function () {
alert(this.firstname + ' ' + this.lastname)
}
}
alert(aPerson.firstname); // undefined
The prototype assignment will add new behavior to the existing properties and functions instead of overwriting it.
function Person()
{
this.surname = "zainal";
this.givenname = "aripin";
}
Person.prototype.city = "sidoarjo";
Person.prototype = {
firstname : "zainal",
lastname : "aripin",
show: function () {
alert(this.firstname + ' ' + this.lastname)
}
}
var aPerson = new Person();
alert(aPerson.surname); //zainal
alert(aPerson.givenname); //aripin
alert(aPerson.city); //sidoarjo
alert(aPerson.firstname); //zainal
alert(aPerson.lastname); //aripin