gpt4 book ai didi

javascript - 在为保存流的变量分配新值之前,我们是否需要从流中删除监听器?

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:19 25 4
gpt4 key购买 nike

在用另一个变量替换它之前,我们是否需要删除附加到变量的监听器?

这是一个伪用例:

class Writer {
constructor() {
this.extention = 0;
this.changeFile();
}

changeFile() {
this.extention++;

// Do we need to remove the listeners from the previous stream, or will nodeJS be able to do it automatically since the stream will be destroyed by the GC ?
this.file = fs.createWriteStream('file-'+this;extention+'.txt');

this.file.on('error', doSomething);
this.file.on('drain', doSomething);
this.file.on('finish', doSomething);
}
}

var writer = new Writer();

setInterval(function() {
// ...

writer.changeFile();
}, 10000);

最佳答案

如果您在任何地方都没有该流的引用,垃圾收集器将删除事件和对象。

有一个不错的thread about this

关于javascript - 在为保存流的变量分配新值之前,我们是否需要从流中删除监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com