Flash Tip: Bracket [] Syntax

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:

myNameObject = new Object();
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


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


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值