本文翻译自:Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. 我对JavaScript的delete操作符有点困惑。 Take the following piece of code: 采取以下代码:

var obj = {helloText: "Hello World!"
};var foo = obj;delete obj;

After this piece of code has been executed, obj is null , but foo still refers to an object exactly like obj . 在执行了这段代码之后, objnull ,但是foo仍然引用与obj完全相同的obj I'm guessing this object is the same object that foo pointed to. 我猜这个对象与foo指向的对象相同。

This confuses me, because I expected that writing delete obj deleted the object that obj was pointing to in memory—not just the variable obj . 这让我感到困惑,因为我期望写delete obj删除了obj指向内存的对象 - 而不仅仅是变量obj

Is this because JavaScript's Garbage Collector is working on a retain/release basis, so that if I didn't have any other variables pointing to the object, it would be removed from memory? 这是因为JavaScript的垃圾回收工作在保留/释放的基础,所以,如果我没有指向对象的任何其他变量, 它从内存中删除?

(By the way, my testing was done in Safari 4.) (顺便说一句,我的测试是在Safari 4中完成的。)


#1楼

参考:https://stackoom.com/question/37Bn/在JavaScript中删除对象


#2楼

based on @Guffa 's answer. 根据@Guffa的回答。 I found the following method works for me: 我发现以下方法适合我:

var obj = {helloText: "Hello World!"
};obj = null;delete obj;

By setting the obj to null first, you removed all the reference to it, then you can delete it completely. 通过首先将obj设置为null ,您删除了对它的所有引用,然后您可以完全删除它。

I didn't test it on other browser, but this works in phonegap 1.7.0 我没有在其他浏览器上测试它,但这适用于phonegap 1.7.0


#3楼

Aside from the GC questions, for performance one should consider the optimizations that the browser may be doing in the background -> 除了GC问题,对于性能,我们应该考虑浏览器可能在后台进行的优化 - >

http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/ http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/

It appears it may be better to null the reference than to delete it as that may change the behind-the-scenes 'class' Chrome uses. 看起来删除引用可能比删除它更好,因为这可能会改变Chrome使用的幕后“类”。


#4楼

I stumbled across this article in my search for this same answer. 我在寻找同样的答案时偶然发现了这篇文章。 What I ended up doing is just popping out obj.pop() all the stored values/objects in my object so I could reuse the object. 我最终做的只是弹出obj.pop()我对象中的所有存储值/对象,以便我可以重用该对象。 Not sure if this is bad practice or not. 不确定这是不好的做法。 This technique came in handy for me testing my code in Chrome Dev tools or FireFox Web Console. 这种技术对我在Chrome开发工具或FireFox Web控制台中测试代码非常方便。


#5楼

Setting a variable to null makes sure to break any references to objects in all browsers including circular references being made between the DOM elements and Javascript scopes. 将变量设置为null可确保在所有浏览器中中断对对象的任何引用,包括在DOM元素和Javascript范围之间进行循环引用。 By using delete command we are marking objects to be cleared on the next run of the Garbage collection, but if there are multiple variables referencing the same object, deleting a single variable WILL NOT free the object, it will just remove the linkage between that variable and the object. 通过使用delete命令,我们在下一次运行Garbage集合时标记要清除的对象,但是如果有多个变量引用同一个对象,则删除单个变量将不释放该对象,它将只删除该变量之间的链接和对象。 And on the next run of the Garbage collection, only the variable will be cleaned. 并且在下一次运行垃圾收集时,只会清除变量。


#6楼

Just found a jsperf you may consider interesting in light of this matter. 刚刚发现了一个jsperf你可能会认为这件事很有趣。 (it could be handy to keep it around to complete the picture) (保持它完成图片可能很方便)

It compares delete , setting null and setting undefined . 它比较delete ,设置null和设置undefined

But keep in mind that it tests the case when you delete/set property many times. 但请记住,当您多次删除/设置属性时,它会测试这种情况。

在JavaScript中删除对象相关推荐

  1. 对象删除某个属性_充分了解JavaScript中【对象】的概念(二)

    点击上方「蓝字」关注我们 之前的文章: 充分了解JavaScript中[对象]的概念(一) 这篇文章我们继续来讲解JavaScript中[对象]的概念,因为这是一系列的文章,所以建议从第一篇文章开始看 ...

  2. Javascript中的对象和原型(一)(转载)

    面向对象的语言(如Java)中有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,JavaScript 没有类的概念,因此它的对象也与基于类的语言中的对象有所不同. 要了解面向对象,首 ...

  3. 如何从JavaScript中的对象数组中获得不同的值?

    本文翻译自:How to get distinct values from an array of objects in JavaScript? Assuming I have the followi ...

  4. JavaScript中的对象比较[重复]

    本文翻译自:Object comparison in JavaScript [duplicate] This question already has answers here : 这个问题已经在这里 ...

  5. JavaScript中DOM对象的详解

    *** JavaScript中DOM对象的详解*** DOM对象:Document Object Model,文档对象模型.也称为document(文档对象),是HTML页面当前窗体的内容,是连接JS ...

  6. JavaScript中Object对象方法超详细讲解举例说明仅此一篇

    JavaScript中Object对象方法超详细讲解举例说明仅此一篇 Object.assign() Object.create() Object.values() Object.entries() ...

  7. es6删除对象的属性_javascript - 按对象属性从数组中删除对象

    javascript - 按对象属性从数组中删除对象 var listToDelete = ['abc', 'efg']; var arrayOfObjects = [{id:'abc',name:' ...

  8. 创建健壮的isArray()函数(JavaScript中判断对象类型的种种方法)

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...

  9. 如何在Javascript中访问对象的第一个属性?

    本文翻译自:How to access the first property of an object in Javascript? Is there an elegant way to access ...

最新文章

  1. Linux定时备份mysql数据库
  2. 探索移动端的搜索设计
  3. Markdown编辑器editor.md的使用
  4. linux下free命令详解
  5. hdu 4289(最小割最大流定理)
  6. [转] Bookmarklet(书签工具)编写指南
  7. MVC 中Simditor上传本地图片
  8. StatusBar style的那点事
  9. java中的守护线程
  10. java 常用 函数_java在线报表中有哪些常用函数
  11. 吴恩达《机器学习》第十三章:聚类
  12. java server 参数_java serversocket参数详解
  13. SSM-jsp页面放在web-INF下受保护,读取出现404页面tomcat获取不到资源-问题解决配置docBase
  14. iOS 开发之 GCD 不同场景使用
  15. ACDSee Photo Studio 8 for Mac v8.0.2283 数字图象编辑处理软件
  16. 经济机器是如何运行的(观后感)
  17. IE不兼容HTML5、CSS3解决方法
  18. 去除百度搜索列表中广告的方法-电脑端
  19. beyondCompare this license key has been revoked密钥被撤销
  20. 【图解版】深入web请求过程

热门文章

  1. Novodex2.6.2 布料系统
  2. python ui bs_Guibs的Python学习_列表
  3. PHP学习笔记-数组
  4. Zookeeper知识汇总
  5. 高级Android开发面试汇总
  6. postman断言测试脚本一
  7. html隐藏二级导航的制作代码,HTML+CSS实现二级导航
  8. 鸿蒙武器排行榜,刺客伍六七武器排行榜,剪刀垫底,魔刀仅排第二,图四才是最强武器...
  9. etcd 访问 锁_在系统中用etcd实现服务注册和发现
  10. linux怎么复制粘贴_linux中的实用技巧和快捷键总结