<small id='QvhXI'></small><noframes id='QvhXI'>

      1. <tfoot id='QvhXI'></tfoot>
      2. <legend id='QvhXI'><style id='QvhXI'><dir id='QvhXI'><q id='QvhXI'></q></dir></style></legend>
        • <bdo id='QvhXI'></bdo><ul id='QvhXI'></ul>
        <i id='QvhXI'><tr id='QvhXI'><dt id='QvhXI'><q id='QvhXI'><span id='QvhXI'><b id='QvhXI'><form id='QvhXI'><ins id='QvhXI'></ins><ul id='QvhXI'></ul><sub id='QvhXI'></sub></form><legend id='QvhXI'></legend><bdo id='QvhXI'><pre id='QvhXI'><center id='QvhXI'></center></pre></bdo></b><th id='QvhXI'></th></span></q></dt></tr></i><div id='QvhXI'><tfoot id='QvhXI'></tfoot><dl id='QvhXI'><fieldset id='QvhXI'></fieldset></dl></div>

        我什么时候应该在 JavaScript 中使用 delete vs 将元素

        时间:2023-10-12
        <legend id='lHxBt'><style id='lHxBt'><dir id='lHxBt'><q id='lHxBt'></q></dir></style></legend>
      3. <i id='lHxBt'><tr id='lHxBt'><dt id='lHxBt'><q id='lHxBt'><span id='lHxBt'><b id='lHxBt'><form id='lHxBt'><ins id='lHxBt'></ins><ul id='lHxBt'></ul><sub id='lHxBt'></sub></form><legend id='lHxBt'></legend><bdo id='lHxBt'><pre id='lHxBt'><center id='lHxBt'></center></pre></bdo></b><th id='lHxBt'></th></span></q></dt></tr></i><div id='lHxBt'><tfoot id='lHxBt'></tfoot><dl id='lHxBt'><fieldset id='lHxBt'></fieldset></dl></div>
          <tbody id='lHxBt'></tbody>

      4. <tfoot id='lHxBt'></tfoot>

          <small id='lHxBt'></small><noframes id='lHxBt'>

          • <bdo id='lHxBt'></bdo><ul id='lHxBt'></ul>

                1. 本文介绍了我什么时候应该在 JavaScript 中使用 delete vs 将元素设置为 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  可能重复:
                  在 JavaScript 中删除对象

                  我有一个具有大量属性的 JS 对象.如果我想强制浏览器垃圾收集这个对象,我需要将这些属性中的每一个都设置为 null 还是需要使用 delete 运算符?两者有什么区别?

                  I have a JS object having a large number of properties. If I want to force the browser to garbage collect this object, do I need to set each of these properties as null or do I need to use the delete operator? What's the difference between the two?

                  推荐答案

                  在 JavaScript 中没有强制垃圾回收的方法,你也不需要这样做.x.y = null;delete x.y; 都消除了 xy 之前值的引用.该值将在必要时被垃圾回收.

                  There is no way to force garbage collection in JavaScript, and you don't really need to. x.y = null; and delete x.y; both eliminate x's reference to the former value of y. The value will be garbage collected when necessary.

                  如果您将某个属性设为空,它仍会被视为对象上的设置"并会被枚举.我唯一能想到您希望在哪里delete 是如果您要枚举 x 的属性.

                  If you null out a property, it is still considered 'set' on the object and will be enumerated. The only time I can think of where you would prefer delete is if you were going to enumerate over the properties of x.

                  考虑以下几点:

                  var foo = { 'a': 1, 'b': 2, 'c': 3 };
                  
                  console.log('Deleted a.');
                  delete foo.a
                  for (var key in foo)
                    console.log(key + ': ' + foo[key]);
                  
                  console.log('Nulled out b.');
                  foo['b'] = null;
                  for (var key in foo)
                    console.log(key + ': ' + foo[key]);
                  

                  此代码将产生以下输出:

                  This code will produce the following output:

                  Deleted a.
                  b: 2
                  c: 3
                  Nulled out b.
                  b: null
                  c: 3
                  

                  这篇关于我什么时候应该在 JavaScript 中使用 delete vs 将元素设置为 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JavaScript 运行时如何表示闭包和作用域 下一篇:将对象设置为 null 时的 JavaScript(ES6)WeakMap 垃圾回

                  相关文章

                  最新文章

                2. <small id='SpuS7'></small><noframes id='SpuS7'>

                  <i id='SpuS7'><tr id='SpuS7'><dt id='SpuS7'><q id='SpuS7'><span id='SpuS7'><b id='SpuS7'><form id='SpuS7'><ins id='SpuS7'></ins><ul id='SpuS7'></ul><sub id='SpuS7'></sub></form><legend id='SpuS7'></legend><bdo id='SpuS7'><pre id='SpuS7'><center id='SpuS7'></center></pre></bdo></b><th id='SpuS7'></th></span></q></dt></tr></i><div id='SpuS7'><tfoot id='SpuS7'></tfoot><dl id='SpuS7'><fieldset id='SpuS7'></fieldset></dl></div>

                        <bdo id='SpuS7'></bdo><ul id='SpuS7'></ul>

                    1. <tfoot id='SpuS7'></tfoot>
                      <legend id='SpuS7'><style id='SpuS7'><dir id='SpuS7'><q id='SpuS7'></q></dir></style></legend>