Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Remove a variable

    If I use:

    function test(){...}

    testInstance = new test();

    This variable named testInstance will become a global scope variable.

    Can I removed it later if I do not need it?


    (I mean if I have:

    testInstance1 = new test();
    testInstance2 = new test();
    ...

    testInstance10 = new test();

    For whatever purpose I need in my app, a lot of variable like testInstanceX will be created at the global level. So it may take some memory. I wonder If I can remove them once I do not need them, so to free the memory.)


    Thanks


    Scott

    (For a local variable such as

    function test(){

    var localVar;

    }

    Once it is out of the function scope, I believe the localVar will be automatically removed from the memory so I do not need to worry about it.

    )

  • #2
    New Coder
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    testInstanceX = false; ??

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    1: You don't really have to worry about it.
    2: Javascript has a garbage collector, which means it will kill off certain values (not variables) it can prove will not be used again.

    You probably want to use an array was well. For example:
    Code:
    function Test() {
      this.blah = "blah";
    }
    
    var testInstance = [];
    for(var i = 0; i < 100; ++i)
      testInstance[i] = new Test();
    
    alert(testInstance[3].blah);
    
    testInstance[3] = null; // garbage collects what its value.
    // NOTE: testInstance[3] still exists and still can be used.
    Trinithis


  •  

    Posting Permissions

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