Reputation: 937
What is bracket syntax and how is it different from dot syntax? Are there any benefits to using bracket syntax over dot syntax? Could you give me an example? I'm new to programming with ActionScript 3.0 and I'm having trouble understanding how bracket syntax works.
Thank you for your help!
Upvotes: 1
Views: 2818
Reputation: 49402
Are there any benefits to using bracket syntax over dot syntax?
Sure there is :
object["foo.bar"] // refers to foo.bar property of object
object.foo.bar // refers to bar property of foo which is a property of object
To resolve such properties of any object with .
you need to use the square bracket notation , because dot notation will interpret it otherwise.
Another difference will be the look-up time . If you use the dot syntax, the compiler will know at compile time that you are accessing a property of that object. If you use the bracket syntax, the actual lookup of the property is done at runtime. Hence :
object[someKey] // the runtime value of someKey will be used to get a property
object.someKey // resolves to someKey property of an object.
Lastly , dot notation is faster than bracket notation.
Upvotes: 4
Reputation: 19748
With regard to objects in AS3 you can use square brackets instead of the dot syntax if you want to use a string or a string variable to reference some property on an object.
For example
var myObj:Object = {someProperty:"Some Value"};
trace(myObj['someProperty']); //Outputs: Some Value
trace(myObj.someProperty); //Outputs: Some Value
var myProperty:String = "someProperty";
trace(myObj[myProperty]); //Outputs: Some Value
for(var property in myObj)
{
trace(myObj[property]); //trace out each properties value of myObj
}
Upvotes: 1