1
function Dog(name){
    this.name=name;
}
let dogA=new Dog('Paul');
 
Dog.prototype.eat=true;
dogA.eat; //true

Now I destroy Dog.prototype, why dogA.eat still there?

Dog.prototype=null;
dogA.eat;// Still true?

I'm thinking dogA.eat inherited from Dog.prototype. Now Dog.prototype is gone. How can dogA.eat still exist?

qqwenwenti
  • 63
  • 5
  • 1
    `Dog.prototype = null;` doesn’t “destroy” the prototype, it just reassigns the `prototype` property to something else, just like in `let a = {}; const b = a;`, `a = null;` doesn’t affect `b`, doesn’t mutate the object, and doesn’t destroy anything. The original object is still there. – Sebastian Simon Apr 04 '21 at 08:41
  • Still confused since prototype is something else now, why the prototype chain not destoryed? – qqwenwenti Apr 04 '21 at 08:54
  • See [JavaScript Prototype - Please Clarify](/a/49042583/4642212) and [Understanding the difference between Object.create() and new SomeFunction()](/a/4166723/4642212). See also [How value to \_\_proto\_\_ is assigned in javascript?](/q/45566298/4642212). `Dog.prototype = null;` only affects _newly_ constructed objects. The prototype of existing objects is never touched by this reassignment. The [spec](https://tc39.es/ecma262/#sec-terms-and-definitions-prototype) answers all these questions in more detail. – Sebastian Simon Apr 04 '21 at 08:56
  • ```delete Dog.prototype.eat``` will delete the property. Assigning null will affect new objects. – A.T. Apr 08 '21 at 06:12
  • Yea I understand the point now. The key concept here is that the original `Dog.prototype` is always there like a normal object. And `dogA.__proto__` is pointing to the object's memory location all the time. Now `Dog.prototype` points to `nul`l in a new memory location (like, initializing again), but dogA.__proto__ is still pointing to the old memory location, so `eat` is still `true`. End of story. – qqwenwenti Apr 08 '21 at 07:08

3 Answers3

0

The property eat is stored in dogA.__proto__. When you use new operator to create an instance dogA of Dog, javascript will do dogA.__proto__ = Dog.prototype = someObject. Now you make Dog.prototype = null, but dogA.__proto__ still equals to someObject. So dogA.eat still be true. If you make dogA.__proto__ = null, then dogA.eat will be undefined.

0

To destroy the prototype chain, you need to use Object.setPrototypeOf(dogA, null).

Nulling Dog.prototype will only affect new instances created by new Dog, not the Object.getPrototypeOf(dogA) which still is the object with the eat property.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Well, after being stuck for days I realize it basically has nothing to do with prototype. It's all about pointer and memory.

The original memory of Dog.prototype stores eat:true. Then Dog.prototype got re-initialized and pointed to a new memory which stores null. On the other hand, dogA.__proto__ still points to the original memory which contains eat:true all the time. Therefore, the re-initializing of Dog.prototype won't affect dogA.__proto__, cuz they are at different memory locations in the end.

qqwenwenti
  • 63
  • 5