What is the lifetime of an object in javascript code using prototype inheritance?

I am currently reading "Javascript Good Parts" and I came across the next paragraph

If we try to get the property value from the object, and if the object lacks the property name, then JavaScript tries to restore the property value of the prototype object. And if this object lacks ownership, then it moves on to its prototype and so on until the process finally ends with Object.prototype.

If I create an obj2 object from obj1 as a prototype, does this mean that obj1 cannot be destroyed until obj2 also goes out of scope?

+8
javascript prototype
source share
1 answer

While you built object inheritance (linked prototypes), I don't think the browser relies on your links to this object.

ex1:

var a = function(){}; a.prototype.toString = function(){return "I'm an A!";}; var b = new a(); a = undefined; var c = new a();// error => a is not a function any more! b.toString();// it works because the prototype is not destroyed, // only our reference is destroyed 

ex2:

 var a = function(){}; a.prototype.toString = function(){return "I'm an A!";}; var b = function(){}; b.prototype = new a(); a = undefined; var c = new b(); console.log(c+'');// It still works, although our // initial prototype `a` doesn't exist any more. 

UPDATE: This behavior may be due to the fact that in javascript you cannot precisely destroy the object; You can remove all links to it. After that, the browser decides how to handle it without objects Garbage collector .

+5
source share

All Articles