Ankur Aggarwal
Ankur Aggarwal

Reputation: 3101

Why old prototype properties are still available after replacing the original prototype object

I know that Prototype properties are dynamic in nature and only reference of the same is passed by the object. Whenever you change them, the show the changed result at the runtime. But in case of redefining the prototype object, objects created before redefining of prototype still points to the old prototype object. Take this example for example:

var Person=function(firstName,lastName)
{
    this.firstName=firstName;
    this.lastName=lastName;
} 

Person.prototype.getFullName=function()
{
    return this.firstName+" "+this.lastName;
}
var student=new Person("Ankur","Aggarwal");
student.getFullName() //returns "Ankur Aggarwal"

Person.prototype.getFullName=function()
{
    return "I am changed";
}

student.getFullName() //returns "I am changed"

I understand the scenario till here. But if I redefine the prototype again, student doesn't points to the new prototype. It still points to the old prototype only

Person.prototype={}
student.getFullName() //still works why?

I searched on web regarding this but unable to understand the same. Please help me out

Upvotes: 0

Views: 55

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074555

Because there is no direct link between the instance created and the Person.prototype property, just the object that it refers to. When you do new Person(), the resulting instance's underlying prototype is set to the object Person.prototype refers to at that time.

It's the same reason I still get 5 here:

var a = [1, 2, 3, 4, 5];
var b = a;
var a = []
console.log(b.length);

ASCII-art can help here. :-)

Initially, you have the Person function with a prototype property referring to an object that has the getFullName function:

+-----------+
|  Person   |
+-----------+     +-------------+
| prototype |---->|  (object)   |
+-----------+     +-------------+     +------------+
                  | getFullName |---->| (function) |
                  +-------------+     +------------+
                                      | ...        |
                                      +------------+

Then you create an instance of it using var student=new Person("Ankur","Aggarwal");:

+-----------+
|  Person   |
+-----------+     +-------------+
| prototype |--+->|  (object)   |
+-----------+  |  +-------------+     +------------+
               |  | getFullName |---->| (function) |
               |  +-------------+     +------------+
+-----------+  |                      | ...        |
|  student  |  |                      +------------+
+-----------+  |
| __proto__ |--+
| ...       |
+-----------+

Then you give Person.prototype a new value, but that has no effect on student's connection to its underlying prototype:

+-----------+
|  Person   |
+-----------+     +-------------+
| prototype |---->|  (object)   |
+-----------+     +-------------+

+-----------+
|  student  |
+-----------+      +-------------+
| __proto__ |----->|  (object)   |
| ...       |      +-------------+     +------------+
+-----------+      | getFullName |---->| (function) |
                   +-------------+     +------------+
                                       | ...        |
                                       +------------+

Side note: __proto__ is not literally a property name, although it will be as of ES6. In ES5 and earlier, an object's underlying prototype wasn't accessible as a property except on engines that had a non-standard extension.


I said there's no direct link between the instance and Person.prototype, which is true. There is an indirect link, but it's not really relevant to the question. Stop reading now if you're not a pedant. Okay, for you pedants: The initial object Person.prototype refers to has a property, constructor, that refers to Person. So even after Person.prototype has been updated, student has its link to its prototype, which has a constructor property referring to Person, which has Person.prototype. Doesn't affect the question, but I thought I'd mention it.

Upvotes: 1

Related Questions