Clearing LoadVars() in ActionScript

The LoadVars() object is a funny thing. It’s basically a bucket for variables that you’d like to load into and send from an SWF file, as well as any methods and event handlers you invoke.

LoadVars() can be problematic, however, if you ever find yourself reusing the same object for repeated load() or sendAndLoad() operations. As explained in the ActionScript 2.0 Language Reference:

Downloads variables from the specified URL, parses the variable data, and places the resulting variables in my_lv. Any properties in my_lv with the same names as downloaded variables are overwritten. Any properties in my_lv with different names than downloaded variables are not deleted.

In other words, if you continue reusing the same LoadVars() object, you’re going to have more data floating around than you may need at a time. Let’s take a look.

First we’ll request a file containing variables with a load() operation.

Any more load or sendAndLoad operations will only add properties to the object unless you overwrite them — or delete them.

Before deleting anything from your LoadVars object, however, copy the properties and values to another variable. Arrays and objects are a good fit here. You can create an array that only copies the variable-value pairs that you need to keep, then delete each variable from the LoadVars object, emptying it for reuse.