【发布时间】:2014-09-04 20:15:36
【问题描述】:
我是 JavaScript 的新手,并尝试使用以下 Mozilla 参考资料:MDN Memory Management 了解与对象相关的内存管理。
我正在关注一个示例,但在理解参考资料时遇到了问题。
var o = {
a: {
b:2
}
};
// 2 objects are created. One is referenced by the other as one of its property.
// The other is referenced by virtue of being assigned to the 'o' variable.
// Obviously, none can be garbage-collected
var o2 = o; // the 'o2' variable is the second thing that
// has a reference to the object
o = 1; // now, the object that was originally in 'o' has a unique reference
// embodied by the 'o2' variable
var oa = o2.a; // reference to 'a' property of the object.
// This object has now 2 references: one as a property,
// the other as the 'oa' variable
o2 = "yo"; // The object that was originally in 'o' has now zero
// references to it. It can be garbage-collected.
// However what was its 'a' property is still referenced by
// the 'oa' variable, so it cannot be free'd
oa = null; // what was the 'a' property of the object originally in o
// has zero references to it. It can be garbage collected.
我对 这个对象、一个被另一个引用、创建了 2 个对象等术语感到困惑 - 为了什么? 'o' & 'a'?,表示对对象的引用 - 哪个对象?
有人可以用实际的对象名称改写上面的 cmets 吗?
这可能被视为一个 spoonfeeding 问题,但如果不值得问这个问题,请告诉我。我会删除它。
【问题讨论】:
-
对象没有名字;这就是重点。要么有变量(或对象属性)引用了一个对象,要么没有。
-
JavaScript 有对象的文字符号。这些对象可以有其他对象作为它们的属性。
标签: javascript object memory-management garbage-collection