4

Summary

I want to use class as a property name of an object, and access it using dot notation.

Preamble

I already know that, according to ECMA-262 5.1 Edition / June 2011 (page number 19, page 31), class is a Future Reserved Word:

7.6.1.2 Future Reserved Words

(...) Syntax

FutureReservedWord :: one of
    class    enum    extends    super
    const    export  import

But, according to ECMA-262 5.1 Edition / June 2011 (page number 17, page 29), the restriction of not being a ReservedWord only affects Identifier, but not IdentifierName:

7.6 Identifier Names and Identifiers

(...) An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1). (...)

Syntax

Identifier ::
    IdentifierName but not ReservedWord

And, according to ECMA-262 5.1 Edition / June 2011 (page number 67, page 79), an object property name is an IdentifierName instead of an Identifier:

11.2.1 Property Accessors

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> ]

Problem

The problem is that some non-compliant browsers throw an error when using reserved words as property names:

var obj = {class: foo};   // error
var obj = {'class': foo}; // ok
obj.class;                // error
obj['class'];             // ok

What I want

Of course, I could use bracket notation instead of dot notation, but it becomes ugly with methods, because there are too many brackets:

o.if().class('foo').implements('bar').use().native().interface() // looks good
o['if']()['class']('foo')['implements']('bar')['use']()['native']()['interface']() //ugly

Then, I want a dirty hack (like HTC behaviours or things like that) that, when included using conditional comments, it makes dot notation work with reserved words, on IE8.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Oriol
  • 274,082
  • 63
  • 437
  • 513

0 Answers0