最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In jQuery how delete variables and functions - Stack Overflow

programmeradmin2浏览0评论

I've got a function in jQuery, eg.

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;

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?

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.

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.

I've got a function in jQuery, eg.

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;

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?

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.

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.

Share Improve this question edited Aug 2, 2011 at 12:49 Répás asked Aug 2, 2011 at 12:40 RépásRépás 1,8207 gold badges28 silver badges55 bronze badges 4
  • 1 Note you called it asd not $asd. – pimvdb Commented Aug 2, 2011 at 12:41
  • Maybe I don't understand the question, but aren't local variables deleted or marked for garbage collection after you exit the function? – JJJ Commented Aug 2, 2011 at 12:42
  • 1 Are you asking how to call delete on a function? Tried delete window['dosomething']? – sorpigal Commented Aug 2, 2011 at 12:43
  • Maybe, if i put the whole javascript into a global variable, it will execute inside the variable doesn't it? So i can clear the function by resetting the global variable which contains the function. ? – Répás Commented Aug 2, 2011 at 12:53
Add a comment  | 

2 Answers 2

Reset to default 27

Set it to undefined:

asd = undefined;

Update To unset a function, do:

myFunction = undefined;

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

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

Hope this will help You.

发布评论

评论列表(0)

  1. 暂无评论