Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser support for using a reserved word as a property name in JavaScript

Tags:

javascript

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?

like image 640
Alex Korban Avatar asked Mar 15 '11 00:03

Alex Korban


2 Answers

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

like image 78
Alex Korban Avatar answered Oct 15 '22 23:10

Alex Korban


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.

like image 41
Alex Wayne Avatar answered Oct 16 '22 01:10

Alex Wayne