Javascript 在jQuery中如何删除变量和函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6912216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:44:16  来源:igfitidea点击:

In jQuery how delete variables and functions

javascriptjqueryoptimizationvariablesfunctional-programming

提问by Répás

I've got a function in jQuery, eg.

我在 jQuery 中有一个函数,例如。

var asd;

function dosomething(){
    for (i=0;i<=1000000;i++)
    {
    asd[i] = "(2^i)";
    }
}

How can i unset the variables after the function?

如何在函数后取消设置变量?

delete $asd;

delete $asd;

With this, i can clear the variable from the memory.

有了这个,我可以从内存中清除变量。

But can i reach the function's destructor in jQuery and how can I unset the whole function in the function's destructor?

但是我可以在 jQuery 中访问函数的析构函数,如何在函数的析构函数中取消整个函数的设置?

THE WHY

为什么

The function and all the global variables are in the memory after running a script.
If i run something by the console, after the dom ready - since the all variables are still in the memory - the program will run.

运行脚本后,该函数和所有全局变量都在内存中。
如果我通过控制台运行一些东西,在 dom 准备好之后——因为所有的变量仍然在内存中——程序将运行。

So I'd like to clear the variables on the function's desctructor, then reset the function, or make it to null.

所以我想清除函数析构函数上的变量,然后重置函数,或将其设置为空。

Because it will flush the whole script from the memory, so my page will be faster. Imagine a bit larger data structure than a single function, like 100 functions and 800 global variables. After the shown, i don't need the variables anymore.

因为它会从内存中刷新整个脚本,所以我的页面会更快。想象一下比单个函数大一点的数据结构,比如 100 个函数和 800 个全局变量。显示后,我不再需要变量了。

回答by Adam Hopkinson

Set it to undefined:

将其设置为未定义:

asd = undefined;

UpdateTo unset a function, do:

更新要取消设置功能,请执行以下操作:

myFunction = undefined;

You don't need the brackets. In your comment, you've misspelt undefined

你不需要括号。在您的评论中,您拼错了undefined

回答by Master Stroke

Remove the set of matched elements from the DOM. Use .remove( [selector] ).

从 DOM 中删除匹配的元素集。使用 .remove( [选择器] )。

Hope this will help You.

希望这会帮助你。