Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解JavaScript、Mozilla中的内存管理_Javascript_Object_Memory Management_Garbage Collection - Fatal编程技术网

了解JavaScript、Mozilla中的内存管理

了解JavaScript、Mozilla中的内存管理,javascript,object,memory-management,garbage-collection,Javascript,Object,Memory Management,Garbage Collection,我不熟悉JavaScript,并试图了解与使用此Mozilla引用的对象相关的内存管理: 我在举一个例子,但在理解参考资料时遇到了问题 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' v

我不熟悉
JavaScript
,并试图了解与使用此Mozilla引用的对象相关的内存管理:

我在举一个例子,但在理解参考资料时遇到了问题

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.
我被一些术语弄糊涂了,比如这个对象一个被另一个引用,创建了两个对象-是为了什么o'&'a'?,它引用了对象-哪个对象?

有人能用实际的对象名来重新表述上面的评论吗


它可能被认为是一个调羹问题,但如果不值得问这个问题,请告诉我。我会删除它

这是一个有点蹩脚的解释。我给你一个粗略的版本

var o = { 
  a: {
    b:2
  }
}; 
// 2 objects are created. One (the value of the property named "a")
// is referenced by the other (the value of the variable named "o")
// as its property.
// The other (the value of the variable named "o")
// is referenced by virtue of being assigned to the 'o' variable.
// Obviously (maybe to the author...), none can be garbage-collected

var o2 = o; // the 'o2' variable now also
            // has a reference to the object (the value of the variable "o")

o = 1;      // "o" now refers to something else and "o2" is the only variable
            // referring to the original "o" object.

var oa = o2.a; // reference to the 'a' property of the "o2" object.

o2 = "yo"; // The object that was originally in 'o' has now zero
           // references to it, but
           // the object's 'a' property is still referenced by 
           // the 'oa' variable, so the "o2" object cannot yet
           // be GC'ed.

oa = null; // The object now has zero references to it, so it can be
           // garbage collected.

对象没有名称;这就是重点。存在引用对象的变量(或对象属性),或者没有。JavaScript具有对象的文字符号。这些对象可以将其他对象作为其特性。