我目前正在阅读“Javascript Good Parts”,我看到了以下段落
如果我们尝试从对象中检索属性值,并且如果 object缺少属性名称,然后JavaScript尝试检索 原型对象的属性值。如果那个对象是 缺乏财产,然后它去原型,等等,直到 该过程最终使用Object.prototype达到最低点。
如果我从obj1创建一个obj2对象作为原型,这是否意味着在obj2超出范围之前不能销毁obj1?
答案 0 :(得分:5)
只要您构建了对象的继承(链接原型),我认为浏览器不依赖于您对该对象的引用。
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.
<强>更新强> 这种行为可能与javascript中无法准确销毁对象的事实有关;你只能删除它的所有引用。之后,浏览器决定如何通过它Garbage collector处理未引用的对象。