开发者

destroy a function in javascript (jquery)

Is there anyone who knows how to destroy a javascript (jquery) function? I'm using jquery "selectable" and a function call "edit" is fired on selectable "stop" event.

Inside this "edit" function I have nested switch functions with a lot of "click" events and I have many functions within each "click" event. My problem is, every time I fire the "selectable" functions and events inside the function "edit" is fired again but the previous functions and events still exist. What i do now is to unbind every event in the function "edit" on selectable "start" even.

Is this a memory leak problem? and is there a way to "des开发者_C百科troy" functions in javascript? i have tried to declare the function to null when the function ends but this does not work. functions and events inside it still exist.

anyone have a clue?

demo page here --> http://dreamerscorp.com/test/test01/javascript_destory_test.html

edit 2009/10/31 :) thanks a lot for your helps, your comments are very useful to me, thanks again!!!


You can try to nullify the function, or override it assigning an anonymous function that does nothing:

myFunction = null;
// or 
myFunction = function () {};

You can also do it within the function itself:

var autoDestroy = function () {
  autoDestroy = null;
  //...
  return 1;
};

autoDestroy(); // returns 1
autoDestroy(); // TypeError: autoDestroy is not a function


to unbind events in jquery use unbind function http://docs.jquery.com/Events/unbind

$("something").click(function() {
    $("anotherOne").click(function() {
         ....
    });
});

in this example, every time "something" is clicked, an event handler is added to "anotherOne", so if you click three times, you'll get three event handlers.

$("something").click(function() {
    $("anotherOne").unbind('click').click(function() {
         ....
    });
});

here you're guaranteed to have only one click handler on "anotherOne".

There's no need to destroy previous handler explicitly.


Basically you need to remove all references to those functions so that the JavaScript garbage collector can collect them. If they are bound, you need to unbind them. If there are other variables in there that point to them, they need to be set to null.

It might help if you posted some code; then we can give a better answer.

...EDIT:

What's happening here is, you're creating a closure that will outlive the containing function:

    function edit(){
      $('.edit').click(function(){
        //...
        function _edit(boxTitle,selectedItemAmount){
          //...
          $('#box .yes').click(function(){
            alert(boxTitle + ' for ' + selectedItemAmount + ' selected item');
            $('#msg').hide(); // hide msg box when yes btn is clicked
          });
        }
        //...
        $('#box .no').click(function(){
          $('#msg').hide();
        });
      });

In other words, inside a function, you're saying, "Attach this function to a DOM object," and you're doing it inline. JavaScript captures variables from outer contexts and keeps them alive while the reference to the inner context is alive.

What you need to do is to define the functions somewhere not inline and then use them:

    function boxClickYes(e) {
      alert(e.data.boxTitle + ' for ' + e.data.selectedItemAmount +
        ' selected item');
      $('#msg').hide(); // hide msg box when yes btn is clicked
    }
    function boxClickNo(e) {
      $('#msg').hide();
    }
    function edit(){
      $('.edit').click(function(){
        //...
        function _edit(boxTitle,selectedItemAmount){
          //...
          $('#box .yes').bind("click", {boxTitle: boxTitle,
            selectedItemAmount: selectedItemAmount}, boxClickYes);
        }
        //...
        $('#box .no').click(boxClickNo);
      });

This also demonstrates how to use the data property in jQuery click handlers to store data in between the time you attach the handler and the time you use it (instead of storing that data in a closure that will keep the scope chain in memory). Using inline-defined functions is fine when you're just using it right there (like the body of a $.each, for instance) but it's not OK when you're attaching event handlers.


To "destroy" a function in javascript, simply ensure that the function becomes unreachable. This will enable the function to be eligible for reclamation. One thing to watch out for is that javascript variables are bound based on scopes (not as individual variables) and a scope, with many unused objects, may persist if a binding is kept to the scope: to provide any more help requires knowledge of the specific code and how it is used.

Please see the javascript delete operator as one way of removing a variable or object member. Setting the value to null/undefined/other-object removes on method of reaching the object previously referenced (although it might still be reachable otherwise and thus not be reclaimed) but does not get rid of the variable/member.

delete variable
delete obj.member
delete obj[member]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜