tags:

views:

12

answers:

1

I receive error "SyntaxError: Unexpected token default" while getting or setting attribute "default" on any object in Chrome (+Opera).

({}).default

Can someone explain why this happens ?

upd: everybody, plz, becareful it doesn't make sense for FF so while testing you can miss this bug.

+1  A: 

default is a keyword in JavaScript (for the switch statement).

Using reserved words in dot property accessors is allowed in ECMAScript Fifth Edition (specifically: the syntax for the dot property accessor is MemberExpression . IdentifierName [section 12.2.1], and IdentifierName, unlike Identifier, may be a ReservedWord [section 7.6]), but not in ECMAScript Third Edition (where it's MemberExpression . Identifier). Third Edition is currently the baseline all browsers support.

For safety use [] property access:

({})['default']= ...;

or avoid using keywords as property names.

bobince