javascript - Why old prototype properties are still available after replacing the original prototype object -
I know that prototype properties are dynamic in nature and only those references have been passed by the object. Whenever you change them, show the change result at runtime, but in the case of redefining the prototype object, the object created before redefining the prototype still indicates 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", "Agarwal"); Student.getFullName () // Returns "Ankur Agarwal" Person.prototype.getFullName = function () {return "I am changing"; } Student.getFullName () // Returns "I'm changing" I understand even the scenario. But if I redefine the prototype again, then the student does not point to the new prototype. It still indicates the old prototype
student.prototype = {} student.getFullName () // Why still works? I searched the web about this but unable to understand it. Please help me
Because there is no direct connection between this example and person prototype < / Code> Properties , simply refers to that object when you do new person () , the resultant example of the underlying prototype Person.prototype at that time is referred to as code> object. This is the 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: -) In the beginning, you have a prototype with the person function in the context of an object in the property getFullName function: + ----------- + | Person + ----------- + + ------------- + | Prototype | ---- & gt; | (Object). + ----------- + + ------------- + ------------ + | GetFullName | ---- & gt; | (Function) | + ------------- + ------------ + | ... | + ------------ + Then you can make an example by using the var student = new person ("Ankur", "Agarwal"); : + ----------- + | Person + ----------- + + ------------- + | Prototype | - + - & gt; | (Object). + ----------- + | + ------------- + ------------ + | | GetFullName | ---- & gt; | (Function) | | + ------------- + + ------------ + + ----------- + | | ... | | Students | | + ------------ + + ----------- + | | __proto__ | - + | ... | + ----------- + You then assign a new value to person.prototype , but its effect on student Connection to its underlying prototype: + ----------- + | Person + ----------- + + ------------- + | Prototype | ---- & gt; | (Object). + ----------- + + ------------- + ----------- + | Students | + ----------- + + ------------- + | __proto__ | -----> | (Object). | ... | + ------------- + + ------------ + + ----------- + | GetFullName | ---- & gt; | (Function) | + ------------- + ------------ + | ... | + ------------ +
Side note: __proto___ is not really a property name, although it's like ES6 Will happen. ES 5 and earlier, was built prototype of an object can not be accessed as a standard, which was non-standard extensions except the engine.
I between said direct link is not an example and person.prototype , which really is a indirect link, but this is not really relevant for the question. If you are not schoolman now to stop reading right Padnts your initial object Person.prototype means a property, constructor , which Person refers to . If after updating Person.prototype , student is a link to its prototype, which constructor property is the reference person , which contains person.prototype . Does not affect the question, but I thought I mentioned it.
Comments
Post a Comment