...

View Full Version : Resolved Can you delete a variable?



TheTot
02-01-2009, 10:01 PM
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?

freedom_razor
02-01-2009, 10:10 PM
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.

flynch01
02-01-2009, 10:17 PM
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.

Philip M
02-01-2009, 10:21 PM
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)

TheTot
02-01-2009, 10:45 PM
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:


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.

Philip M
02-02-2009, 02:05 PM
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! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum