如果我不将它保存在任何地方(例如,在数组中),是否可以在内存中找到它。编码:
const testing = () => {
class Obj {
constructor(id, title = 'Title') {
this.id = id,
this.title = title
}
}
for (let i = 0; i < 10; i++) {
const article = new Obj(i);
document.writeln(article.id, ' ', article.title, ';')
}
}
testing();
是否有任何相关的对象,例如:
{id : 5, title : "Title},它们可以通过 id 以某种方式检索吗?
或者旧的在行中被销毁const article = new Obj(i);
由于创建的对象没有保存在任何地方,它们只能在 line之后
const article = new Obj(i);的循环内直接访问。当移动到下一次迭代时,对创建对象的引用丢失,因此,访问它的能力也丢失了。
随后,创建的对象可以被垃圾收集器收集。