18

I'm trying to use "for" as an object property name. It seems to work fine in IE7, IE8, Firefox, Chrome and Opera, but apparently not in Safari.

My understanding is that ECMAScript 5 allows it (as discussed in JavaScript keywords in hash keys).

Can I get a definitive list of browsers that support/don't support this somewhere?

EDIT: Actually, CoffeeScript's auto-stringification of reserved word property names is what led me to believe that it works. After re-testing properly it doesn't seem to work anywhere, so the question now is: are there any browsers that allow it as per ECMAScript 5 specification?

Community
  • 1
  • 1
Alex Korban
  • 14,916
  • 5
  • 44
  • 55

4 Answers4

22

There is a table showing browser support for ECMAScript 5 features here: http://kangax.github.com/es5-compat-table/

Reserved words can be used as property names in IE9, Firefox 3.5+ and Chrome 7+, Safari 5.1+.

Izhaki
  • 23,372
  • 9
  • 69
  • 107
Alex Korban
  • 14,916
  • 5
  • 44
  • 55
14

You can use those words, but only as strings and not shorthand properties.

foo['class']; // cool
foo.class;    // not cool

But here is the actual list you requested. None of these can be used via property dot syntax. https://web.archive.org/web/20140902235313/http://javascript.about.com/library/blreserved.htm


Also, only slightly tangential, CoffeeScript notices this and auto stringifies them for you. http://jashkenas.github.com/coffee-script/#literals

Input:

$('.account').attr class: 'active'
log object.class

JS Ouptput:

$('.account').attr({
  "class": 'active'
});
log(object["class"]);

I happen to think that is pretty dang neat.

Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • I was actually trying to figure out which browsers allow the use of reserved words as property names, if any. Is there a list somewhere? However, +1 for mentioning CoffeeScript. Auto stringification is what led me to believe it worked in most browsers. – Alex Korban Mar 15 '11 at 01:55
  • The non-JSR223 implementation of the Mozilla Rhino JavaScript engine does this when the script throws a JavaException. If you need to do special logic to handle it, you need to check exception.javaException["class"].name to see if it matches the Java class name of the specific exception you're looking for. – sworisbreathing Nov 02 '11 at 15:59
  • 2
    `None of these can be used via property dot syntax.` They can in ES6. See the use of the `catch` keyword as function name in `Promise` objects. – Stijn de Witt Jul 10 '15 at 12:34
5

Perhaps the question is not so much whether reserved words can be used as property names for objects - they can. Seems like the real question here is whether they can be used naked (ie. without being clothed in quotes). Naked use of reserved words is supported from ES5 onwards, but not all interpreters support ES5 features yet. As mentioned, there is a good table here...

http://kangax.github.io/es5-compat-table/#Reserved%20words%20as%20property%20names

If you're just doing some coding in NodeJS, then you don't have to worry about this type of issue. If you care about portability of your code to a wide variety of environments (without using a language like CoffeeScript that compiles to JavaScript), then your code should quote the reserved words...

defining...

var foo = {'for':'this works'};

accessing...

foo ['for'];
UIZE
  • 654
  • 7
  • 2
-2

after study, I think I can prove that using reserved words as property names (if done with a little common sense) is harmless, whether in the obj['name'] or obj.name syntax.

see this stackoverflow question

Community
  • 1
  • 1
cc young
  • 18,939
  • 31
  • 90
  • 148
  • 3
    Not true. In IE8, ```SomeClass.delete = function()``` causes a script error to be thrown. – Irongaze.com May 28 '13 at 17:47
  • way to go! although agree that not recommended, is that behavior standard? have not used ie in years and years, so unfamiliar with that neck of the woods. – cc young May 29 '13 at 08:54
  • 1
    IE9 and later seem to correctly let you use keywords as properties, so I think this falls into the "damn you IE7/8" camp of problems. – Irongaze.com May 29 '13 at 17:47