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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    18
    Thanked 1 Time in 1 Post

    Can you delete a variable?

    Alright I don't know if this is possible, but I want to know if I can tell my javascript to delete a variable. Not set it to "", not set it as null, I want to erase it, tell my javascript that it doesn't even exist.

    Can I do this?
    Last edited by TheTot; 02-01-2009 at 09:49 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    Don't think you actually can. You can try to use a delete operator, but variables declared by user with var cannot be deleted. If you define your variables without the var keyword [x=1;], you can use delete to erase them.

  • Users who have thanked freedom_razor for this post:

    TheTot (02-01-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    You can only use the delete operator to delete variables declared implicitly, but not those declared with var. As for the ones you do declare with var, you can just undefine them.

    thevariable = undefined;

    Kindof does the same thing seeing as javascript is a scripting language and all.

    Edit: Also might be helpful to know that delete can be used to free objects as well, and also specific properties if you wish to.

    Another Edit: There's no point in this, unless you're REALLY memory conscious there is no point to this.
    Last edited by flynch01; 02-01-2009 at 09:24 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Why would you want to do this?
    You cannot destroy (= erase completey) a variable completely. But you can
    set variablename = undefined
    which converts it to type 'undefined' - a special (primitive) type that only has one value, undefined.

    At this point, it is in effect indistinguishable from a variable that
    either hasn't been declared or has been declared but not given a value.
    Does that fit with your concept of 'erased'?


    “I am so clever that sometimes I don't understand a single word of what I am saying.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    18
    Thanked 1 Time in 1 Post
    Thanks guys, the delete operator worked exactly as I needed it to.

    @ all the people asking why I would want to do this:

    I am scripting an RPG. During which, you gain and lose items such as potions. You can only display your item list during battles and while in the item shop (so you know what to buy), so I'm using for (x in itemName) to display and erase the item list. This works fine of course.

    But in the long run, this game will be rather large, and I'm testing adding a save/load feature (which is possible thanks to your guys' suggestion about the delete operator, thanks). You will only be able to save at towns' Inns, and only be able to load at the very beginning of the game.

    When you save, it uses for (x in itemName) to set each itemName, itemQty, itemType, itemRec, and itemDes into a cookie. This, of course, only saves the ones you actually own, which is what I want.

    When you load, however, I obviously can not use for (x in itemName), because they don't exist yet. So I have them load all the possible items in the game, and then I run:
    Code:
    		for ( x in itemName ) {
    			if ( itemName[x] == "" ) {
    					delete itemName[x];
    					delete itemQty[x];
    					delete itemType[x];
    					delete itemRec[x];
    					delete itemDes[x];
    			}
    		}
    This way, while displaying the item list, there's no errors in the status bar trying to find things that don't exist yet.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by TheTot View Post
    When you save, it uses for (x in itemName) to set each itemName, itemQty, itemType, itemRec, and itemDes into a cookie. This, of course, only saves the ones you actually own, which is what I want.

    Cookies can be deleted - and edited!


  •  

    Posting Permissions

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