简体   繁体   English

从 memory node.JS 中删除变量

[英]Deleting variable from memory node.JS

I know, that, for example, c++ has something like delete variable and it will be deleted from the memory.我知道,例如,c++ 有类似delete variable的东西,它将从 memory 中删除。 Is there something like this in JS. JS中有这样的东西吗? For example, I have var canSendRequest = true;例如,我有var canSendRequest = true; and after some operations I want to delete it totally from the memory.经过一些操作后,我想将其从 memory 中完全删除。 As I read, delete canSendRequest will not work properly, but I did not manage to find a properly working way.当我阅读时, delete canSendRequest将无法正常工作,但我没有设法找到一种正常工作的方式。 Does someone know, how to do that?有人知道,该怎么做?

Since JS is garbage collected, you don't have direct control over the release of memory.由于 JS 是垃圾回收的,因此您无法直接控制 memory 的发布。

You just have to wait for the runtime to free the memory, which it will if there are no permanent references to it via the global object or a permanently existing closure.您只需等待运行时释放 memory,如果没有通过全局 object 或永久存在的闭包对其进行永久引用,它将释放。 In those cases, you need to eliminate such references to the variable so that it qualifies to be freed.在这些情况下,您需要消除对变量的此类引用,以便它有资格被释放。

If you're saying you want to eliminate the identifier itself from the scope, you definitely can't unless it was a global that was set as a property of the global object, not using proper variable declaration syntax.如果你说你想从 scope 中删除标识符本身,你肯定不能,除非它是一个被设置为全局 object 属性的全局,而不是使用正确的变量声明语法。

Var is Hoisting type use let or const. Var 是提升类型,使用 let 或 const。 like var g_a = 1; //create with var, g_a is a variable delete g_a; //return false console.log(g_a); //g_a is still 1var g_a = 1; //create with var, g_a is a variable delete g_a; //return false console.log(g_a); //g_a is still 1 var g_a = 1; //create with var, g_a is a variable delete g_a; //return false console.log(g_a); //g_a is still 1

now try with let and const现在尝试使用 let 和 const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM