How do I remove functions from memory in Javascript after using Jquery .html() function to replace a
with a – Atom999 Mar 04 '21 at 15:01

  • I haven't done that so far, but check this topic: https://stackoverflow.com/questions/3853274/how-to-find-javascript-functions-in-a-javascript-js-file , or do some more digging. – ikiK Mar 04 '21 at 15:14
  • 1 Answers1

    1

    JavaScrip has an internal garbage collection and your code does not have to destroy things like you would do in C++

    However, at Certain times we would want to "destroy a function" where it is resources expensive Because js runs from top to bottom you can overwrite that function if you have called it in a variable later in the program to free up those resources. Or even do it later in the logic of the program

       var ExpensiveFunction =( function () {
     //..code
    function getRidOfthis1(){ console.log("foo1"); }
    function getRidOfthis2(){ console.log("foo2"); }
    function getRidOfthis3(){ console.log("foo3"); }
    function getRidOfthis4(){ console.log("foo4"); }
    
    //initiate an internal reference to them 
    ExpensiveFunction.getRidOfthis1 = getRidOfthis1;
    ExpensiveFunction.getRidOfthis2 = getRidOfthis2;
    ExpensiveFunction.getRidOfthis3 = getRidOfthis3;
    ExpensiveFunction.getRidOfthis4 = getRidOfthis4;
    
    }  );
    
    //Functions available outside of the nesting 
    ExpensiveFunction()
    ExpensiveFunction.getRidOfthis1();
    ExpensiveFunction.getRidOfthis2();
    
    // overidding it 
    ExpensiveFunction =0
    
    JonoJames
    • 1,117
    • 8
    • 22
    • 2
      this does not answer the question of "why do my old definitions remain, and how can I remove them without overwriting them one by one" – tucuxi Mar 04 '21 at 00:52
    • Yes But the point is you can place all the function definition nested under one main function and just destroy the holding container of the group of functions he wants to destroy – JonoJames Mar 04 '21 at 00:56
    • Adjusted it to reflect that – JonoJames Mar 04 '21 at 00:58
    • 1
      as written, your functions are not accessible from outside `ExpensiveFunction`. A pitfall of your approach is that, if they are accessible from outside (which you can assume is the case for OP), direct references to those functions would not be affected by setting `ExpensiveFunction` to a different value. – tucuxi Mar 04 '21 at 01:07
    • You have a point there so showed how to initiate those function from outside of the nested scope initialization them as object names much like a constructor – JonoJames Mar 04 '21 at 01:30
    • Thanks for the answers/comments. The issue for me is that I'm working on a legacy product and there are dozens of .html files that may fill my #deleteme div, and each of those have dozens of varying functions. So I can't really re-write the incoming functions. I'm looking for a single way to handle all that gets swapped out in that div by the user. – Atom999 Mar 04 '21 at 14:52