Search code examples
actionscript-3apache-flexsyntaxsquare-bracket

What is bracket syntax?


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!


Solution

  • 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.