Breaking the law - Odd Variable Names
DIGG IT!
0
Comments
Published
Wednesday, June 09, 2004
at
8:01 AM
.
On a recent project I needed a simple way to store name value pairs where the name supported all characters. It is a little unknown fact but the flash player supports this by default.&
By evaluating variable names dynamically, you can use any string of characters as a variable name, even ones that are supposedly illegal(inital numbers, special chars, spaces).
Example:
_level0["Ted's Very Long Illegal Variable Name"] = true
trace(_level0["Ted's Very Long Illegal Variable Name"]) //true
The trick is that you must set and get the values using brace notation. If you try to hard code the string name into a path, the Flash compiler with throw an error. You can also use this for any type of object.
_level0['9032480192839128309812098120981209381098'] = {ted:true}
trace(_level0['9032480192839128309812098120981209381098'].ted) //true
Why would you do this?
I found it useful for caching XML result sets on the client-side. Each result set should be identical for unique url during a user session of the app I am building. So for duplicate requests, I used a client-side cache vs going over the wire again. It made the application much faster in this case. The key is using the url as the 'name' and the result as the 'value'.
_level0.cache = {}
_level0.cache['http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=Macromedia+Flash&btnG=Search'] = "GOOGLE RESULT DATA"
I also used this technique within Icon Builder for saving icon files. The file names can use any character allowing support for long filenames and dot notation.
Cheers,
Ted ;)

0 Responses to “ Breaking the law - Odd Variable Names ”
Post a Comment