It's been a while since I've posted any Flash tips. I recon it's time that changed and I get back to spreading some of that knowledge that's lofting up there in that big empty space I call a brain. This particular tip comes from a recent post on the
ActionScript.org forums .
Brackets evaluates the string it contains and resolves a variable reference for the object preceding the first [. So, for example, _level0["Rect"]; is the same as _level0.Rect; _level0["Rect"+1]; is the same as _level0.Rect1;
Arrays rely on this method of variable resolution since array values are stored in variables whose names are numbers. Normally numbers are not allowed as variable names because dot syntax cannot properly resolve variables names starting with numbers as it assumes it to be a number value, i.e. _level0.3something; screws up because Flash gets to the 3 and thinks the number 3, not a variable name starting with 3. However, this naming restriction can be averted using bracket syntax, _level0["3something"]; and in arrays' cases, is required, someArray[3]. Note that bracket contents do not have to be strings, though a string representation of the contents is used to resolve the variable--this through the toString() method.
All variable types in Flash have a toString method which determines what its value looks like as a string. For numbers, its just a value conversion of number to string, visually not really changing at all. Arrays show a string listing of their values separated by commas and for basic objects, you've probably seen its default as "[object Object]" in a trace in Flash. Bracket syntax uses this string representation for evaluation so someArray[3]; is seen as someArray[(3).toString()]; or someArray["3"]; You can use this to your advantage with other variable types like generic objects. See the following:
The myNameObject variable was used to reference the value property of the myValueObject. In the brackets, Flash evaluated myNameObject into its string representation which was customized for myNameObject to output value giving myValueObject["value"]; That then was converted into a variable reference of myValueObject.value; giving the final trace of 3. Consider the following:
Here the myValueObject was given a property called "[object Object]"--something only possible using bracket syntax. Then, two separate generic objects can be used to retrieve that value since their default string representation results in "[object Object]"
Now, an important thing to understand here is that this is for a single variable name in a single object. Brackets don't evaluate paths. So, a "." does not change scope, it becomes part of the variable name. Example:
That traces undefined because the brackets evaluates the string into a single variable name to be resolved in myValueObject, not a path from that object. So myValueObject["container.value"]; checks for the variable named "container.value" in myValueObject, something that doesn't exist. A container variable exists, and it has its own value variable, but a "container.value" variable doesn't exist directly in myValueObject.
Original Posting in Context
More Info from Kirupaforums
Arrays rely on this method of variable resolution since array values are stored in variables whose names are numbers. Normally numbers are not allowed as variable names because dot syntax cannot properly resolve variables names starting with numbers as it assumes it to be a number value, i.e. _level0.3something; screws up because Flash gets to the 3 and thinks the number 3, not a variable name starting with 3. However, this naming restriction can be averted using bracket syntax, _level0["3something"]; and in arrays' cases, is required, someArray[3]. Note that bracket contents do not have to be strings, though a string representation of the contents is used to resolve the variable--this through the toString() method.
All variable types in Flash have a toString method which determines what its value looks like as a string. For numbers, its just a value conversion of number to string, visually not really changing at all. Arrays show a string listing of their values separated by commas and for basic objects, you've probably seen its default as "[object Object]" in a trace in Flash. Bracket syntax uses this string representation for evaluation so someArray[3]; is seen as someArray[(3).toString()]; or someArray["3"]; You can use this to your advantage with other variable types like generic objects. See the following:
myNameObject = new Object();
myNameObject.toString = function(){
return "value";
}
myValueObject = new Object();
myValueObject.value = 3;
trace(myValueObject[myNameObject]); // traces 3
myNameObject.toString = function(){
return "value";
}
myValueObject = new Object();
myValueObject.value = 3;
trace(myValueObject[myNameObject]); // traces 3
The myNameObject variable was used to reference the value property of the myValueObject. In the brackets, Flash evaluated myNameObject into its string representation which was customized for myNameObject to output value giving myValueObject["value"]; That then was converted into a variable reference of myValueObject.value; giving the final trace of 3. Consider the following:
myGenericObject = new Object();
myOtherGenericObject = new Object();
myValueObject = new Object();
myValueObject["[object Object]"] = 6;
trace(myValueObject[myGenericObject]); // traces 6
trace(myValueObject[myOtherGenericObject]); // traces 6
myOtherGenericObject = new Object();
myValueObject = new Object();
myValueObject["[object Object]"] = 6;
trace(myValueObject[myGenericObject]); // traces 6
trace(myValueObject[myOtherGenericObject]); // traces 6
Here the myValueObject was given a property called "[object Object]"--something only possible using bracket syntax. Then, two separate generic objects can be used to retrieve that value since their default string representation results in "[object Object]"
Now, an important thing to understand here is that this is for a single variable name in a single object. Brackets don't evaluate paths. So, a "." does not change scope, it becomes part of the variable name. Example:
myValueObject = new Object();
myValueObject.container = new Object();
myValueObject.container.value = 9;
trace(myValueObject["container.value"]) // traces undefined
myValueObject.container = new Object();
myValueObject.container.value = 9;
trace(myValueObject["container.value"]) // traces undefined
That traces undefined because the brackets evaluates the string into a single variable name to be resolved in myValueObject, not a path from that object. So myValueObject["container.value"]; checks for the variable named "container.value" in myValueObject, something that doesn't exist. A container variable exists, and it has its own value variable, but a "container.value" variable doesn't exist directly in myValueObject.
Original Posting in Context
More Info from Kirupaforums