Search code examples
javascriptpropertieshost-object

Can x.y and x['y'] ever result in different behavior for Host Objects?


For a normal ("Native") JavaScript objects, where the property name is a valid identifier, the expressions x.y and x["y"] are always semantically equivalent.

Is this equivalency guaranteed for Host Objects?

[..an] object supplied by the host environment to complete the execution environment of ECMAScript.

If not, are there notable exceptions? (In Cocoon, Crosswalk, IE, etc?)

In particular I am interested in the Window (window) and other DOM Objects; and if there are any known "exceptions" to the expected behavior, or if there are any environments in where such is possible.


Solution

  • It should be identical. From Section 11.2.1 (Property Accessors) of the Ecma-262 Edition 5.1, we have:

    Properties are accessed by name, using either the dot notation:

    MemberExpression . IdentifierName 
    CallExpression . IdentifierName
    

    or the bracket notation:

    MemberExpression [ Expression ]
    CallExpression [ Expression ]
    

    The dot notation is explained by the following syntactic conversion:

    MemberExpression . IdentifierName
    

    is identical in its behaviour to

    MemberExpression [ <identifier-name-string> ]
    

    and similarly

    CallExpression . IdentifierName
    

    is identical in its behaviour to

    CallExpression [ <identifier-name-string> ]