null和undefined对于显式使对象不可访问是否有效? [英] Are null and undefined equally valid for explicitly making objects unreachable?

查看:52
本文介绍了null和undefined对于显式使对象不可访问是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据有关相关问题的答案,如果希望它们成为对象,则最好使其明确不可用垃圾收集.
出于所有实际意图和目的,用null值还是undefined值完成是否重要?

According to this answer on a related question, it's best to make an object explicitly unavailable if you want them to be garbage-collected.
For all practical intents and purposes, does it matter whether that's done with a null value or an undefined value?

简而言之,垃圾收集器是否可以平等地访问以下两个对象(无论它们最初引用了什么)?

Shortly put, will both of the below objects (whatever they may have referenced originally) be equally accessible for the garbage collector?

window.foo = null;
window.bar = void 0;

推荐答案

分配的值并不重要:它可能是nullundefined{}42.

It does not matter what value you assign: it might be null, undefined, {} or 42.

重要的是,您需要断开变量和堆中对象之间的连接.

What matters is that you need to break the connection between a variable and an object in heap.

一旦某个对象不可访问,它就是要被收集的候选对象,而不论一天中引用该对象的变量当前具有什么当前值.

As soon as an object is not reachable - it's a candidate for being collected, regardless on what the current value the variable that referred to it one day currently holds.

这是一个来自Google Chrome浏览器的内存快照,仅供参考:

Here is a memory snapshots from a Google Chrome, just for fun:

以及相应的JSFiddle: http://jsfiddle.net/r9b5taxf/

And the corresponding JSFiddle: http://jsfiddle.net/r9b5taxf/

这篇关于null和undefined对于显式使对象不可访问是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆