纯函数上下文保留的Javascript对象

在类的方法中,我这样做:

this.editor = monaco.editor.create(this.elm,{
    cursorWidth: 2,cursorBlinking: 'solid',wordBasedSuggestions: false,rulers: [80],wordWrap: 'bounded',wordWrapColumn: 80,wrappingIndent: 'indent',contextmenu: false,dragAndDrop: false,folding: false,glyphMargin: true,language: 'sigmet',lineNumbers: n => n === 1 ? 'H' : n - 1,minimap: {enabled: false},multiCursorModifier: 'ctrlCmd',renderWhitespace: 'all',})

当使用chrome检查内存时,该类的实例会被该函数的上下文保留,该上下文位于传递给monaco.editor的create函数的对象的lineNumbers属性中。

将arrow函数转换为常规函数并不能解决问题。

将函数移至文件开头,这样可以解决问题:

function lineNumbers(n) {
    return n === 1 ? 'H' : n - 1
}

所以我的问题是为什么纯函数阻止垃圾收集?

julia_cm 回答:纯函数上下文保留的Javascript对象

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3149885.html

大家都在问