www.webdeveloper.com
Results 1 to 11 of 11

Thread: dynamically create and delete a function at runtime

  1. #1
    Join Date
    Jan 2008
    Posts
    9

    dynamically create and delete a function at runtime

    Hi,

    I'm using a 3rd party API that does asynchronous requests for me and directs response to a callback that I specify. Since my code can issue multiple requests concurrently I want some mechanism to match each response to the specific request e.g. attach a unique ID to be returned with the response. The 3rd party API does not allow me to attach any user context to the request so the only way I could figure out how to do this was to dynamically generate a callback function per request and embed the unique id in the body of that function. When the callback is invoked I can use the information to complete my handling of the response.


    1) First of all if anybody has a better idea on how to achieve the above without dynamically generating script I'd be happy to hear it.

    2) Regardless of (1), if using my proposed method repeatedly I will be generating a lot of garbage over time, so I want to clean up after myself. Meaning, to delete the dynamic callback after it completes. Below is an example of how I wanted to do it, but it seems not to work:


    Code:
    function CreateDynamicCallback(callID)
    {
    	var it = eval('callback_' + callID + " = function (response)" +
    	"{" +
    	"	alert(eval('fql_callback_' + callID));" +
    	"	var i = delete eval('fql_callback_' + callID);" +
    	"	alert(i);" + //returns true but does NOT delete function as seen in next instruction
    	"	alert(eval('fql_callback_' + callID));" + //Still returns the eval of the function
    	"}");
    
    	return it;
    }
    It is possible the final alert still works since I am still inside the callstack for that function and that it will be collected later. Any ideas?

    Cheers,
    DB
    Last edited by devboy; 07-01-2010 at 03:56 AM. Reason: mistake

  2. #2
    Join Date
    Jan 2008
    Posts
    9
    UPDATE:

    I've added a interval callback to verify that the function is indeed not deleted after time.
    Indeed it is not deleted even 'later'

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Not very clear what you want, but the functions are in fact properties/methods of the Global Window Object.
    So: to create/change dynamically a function:
    Code:
    <script type="text/javascript">
    window['myFunction']=function(arg){
    alert(arg)
    }
    //test it:
    myFunction('boo');
    </script>
    To "delete" dynamically a function (in fact to nullify it), you can make something similar
    Code:
    <script type="text/javascript">
    function myFunction(arg){
    alert(arg)
    }
    window['myFunction']=function(){null}
    //test it:
    myFunction('boo');
    </script>

  4. #4
    Join Date
    Jan 2008
    Posts
    9
    Hi KOR,

    Done as you suggest. It works. One thing though, after nullifying the function, doing alert(myFunction); would still display the message "function () { }" meaning there IS such a function but bodiless. Was that your intention?

    Cheers,
    DB

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Try:
    Code:
    window['myFunction']=null;
    or
    Code:
    window['myFunction']()=null;

  6. #6
    Join Date
    Jan 2008
    Posts
    9
    Actually I tried that as well before last post same thing.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by devboy View Post
    Was that your intention?
    Well. what is your intention? Is it so important to you to alert the function? What's the use? A null function is a function which does nothing. Isn't it what you need?

    I think that you can not simply destroy the JavaScript objects once they were created, whatever you might understand by that. You can nullify them or send them to the garbage collector, but this is another discussion: were or not the nullified object sent to the garbage in different browsers?
    Last edited by Kor; 07-01-2010 at 10:21 AM.

  8. #8
    Join Date
    Jan 2008
    Posts
    9
    OK, I take it back... window['myFunction']=null; works. I must have been running stale sources in the browser or whatever.

    Thanks for the help Kor!

  9. #9
    Join Date
    Jan 2008
    Posts
    9
    I missed your last post...
    The alert was just to print out object evaluation.
    My intention is this. I need to inject some user context to a 3rd party API call. An API call which does not allow for user context. Since the API call gets a callback function pointer from me at invocation, I chose to dynamically create that callback each time, thus allowing me to 'embed' the user context inside the callback function itself. To allow for multiple concurrent calls of this type, I had to give each of these dynamically generated callbacks a unique name. For a long living application this could cause an issue, where I have a lot of these 'garbage' callbacks that aren't being used. The point of the exercise was how to set this mechanism up so that the callback will actually delete itself once it's done. I'm not 100&#37; sure yet, but it seems that I cannot delete the function from within it's own execution, but I can set a timer to delete each callback once that callback returns.

    Hope this whole thing made more sense.

    Regarding the theoretical question you posed... about garbage collection in different browsers. Was that rhetorical? I mean do you know the answer to that? Cuz as I said the whole point is to do proper garbage collection.
    Last edited by devboy; 07-02-2010 at 04:51 PM.

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by devboy View Post
    I'm not 100&#37; sure yet, but it seems that I cannot delete the function from within it's own execution, but I can set a timer to delete each callback once that callback returns.
    you should be able to; the window.XXX and the function itself are two separate things.

    so, while this won't work:
    Code:
    window.bob=function(){
     delete window.bob;
     alert(bob);
    };
    
    bob()

    the following code with a named function expression will:
    Code:
    window.bob=function bob2(){
     delete window.bob;
     alert(bob2);
    };
    
    bob()


    also, you might try this tool, which can kill a global by reference, not by name:
    Code:
    function kill(args, win){
     if(args.callee){args=args.callee;}
     win=win||window;
     for(var i in win){
      if(win[i] && win[i]==args){ delete win[i]; return i;}
     }
    }
    
    
    
    
    //example 1:
    window.bob=function bob2(){
     kill(bob2)
     alert(window.bob);
    };
    bob();//shows "undefined";
    
    
    //example2:
    window.bob=function bob2(){
     kill(arguments)
     alert(window.bob);//shows "undefined";
    };
    
    bob();
    Last edited by rnd me; 07-03-2010 at 01:13 PM.

  11. #11
    Join Date
    Jan 2008
    Posts
    9
    Tested. Works like a charm.

    Thanks

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles