在javascript中将对象引用设置为null,该对象是否会被垃圾回收

时间:2016-06-26 18:10:25

标签: javascript garbage-collection

我有两个场景,他们都会收集垃圾吗?场景一,它是一个函数对象,第二个场景,它是一个对象。

// Scenario one
function ListNode (val) {
    this.val = val;
    this.next = null;
}

var nodeA = new ListNode(1);
var nodeB = new ListNode(2);
var nodeC = new ListNode(3);

// connect A, B, C
nodeA.next = nodeB;
nodeB.next = nodeC;
// skip B
nodeA.next = nodeC;
// now B's next is still pointing to nodeC
// I want to remove B and let garbage collector collects
// B, will setting B's reference to null work?

nodeB = null;

// Scenario two
var obj = {a: 123, b: 456};
// this obj will be garbage collected if I set the ref to null?
obj = null;

0 个答案:

没有答案