Does ES6 const affect garbage collection?

前端 未结 4 1402
鱼传尺愫
鱼传尺愫 2021-02-02 11:45

In Kyle Simpson\'s new title, You don\'t know JS: ES6 and beyond, I find the following snippet:

WARNING Assigning an object or array as a constan

4条回答
  •  遥遥无期
    2021-02-02 12:28

    The way garbage collectors (GC) work is when something is referenced by nothing ("cannot be reached"), the GC can safely say that something isn't used anymore and reclaim the memory used by that something.

    Being able to replace the value of a variable allows one to remove a reference to the value. However, unlike var, const cannot be reassigned a value. Thus, one can't remove that constant from referencing the value.

    A constant, like a variable, can be reclaimed when the constant goes "out of scope", like when a function exits, and nothing inside it forms a closure.

提交回复
热议问题