Allow reserved words for properties? #61

Closed
ianb opened this Issue Mar 13, 2013 · 5 comments

Comments

Projects
None yet
6 participants
@ianb

ianb commented Mar 13, 2013

Under Objects it says you shouldn't use reserved words. I'm not sure this is important, though avoiding them might make sense for readability. At least using klass instead of class would seem wrong to me, as you are corrupting a word instead of finding a readable synonym (like type).

Right now you can use just about anything. E.g., test = {function: 1}; test.function === 1 –but additionally I think we can be confident this will continue to be the case. IndexedDB uses the method cursor.continue() which isn't just a future-protected reserved word (like class) but a reserved word right now. But IndexedDB represents current API design by people involved in the standardization process.

I think it's questionable from a readability point of view to use current keywords, but I don't see any reason at all to avoid words reserved for possible future use (class and private, as used in the example, are both examples of that).

@hshoff

This comment has been minimized.

Show comment
Hide comment
@hshoff

hshoff Mar 13, 2013

Member

True.

This was one of those Airbnb specific things. We support IE8 and IE8 doesn't support reserved words as property names ([source](http://kangax.github.com/es5-compat-table/#Reserved words as property names)):

var obj = {
  class: function(){}
};

// doesn't work in IE8
obj.class();

You can use them in IE8 if you use the subscript notation:

// this works
obj['class']();

but this contradicts our style guide preference of using dot notation to access properties by name and subscript notation to access properties by variable #properties.

This is another one of those preference decisions where it might sound like "you can't use reserved words as property names" but really what we're trying to say is "we would prefer if you didn't...for now".

Maybe it would be helpful to note what the reserved and future reserved words are and explain that they can be used as Identifiers but not as an IdentifierName ES5 #7.6.1 in [supporting browsers](http://kangax.github.com/es5-compat-table/#Reserved words as property names).

// SyntaxError: unexpected reserved word
function class(){}
Member

hshoff commented Mar 13, 2013

True.

This was one of those Airbnb specific things. We support IE8 and IE8 doesn't support reserved words as property names ([source](http://kangax.github.com/es5-compat-table/#Reserved words as property names)):

var obj = {
  class: function(){}
};

// doesn't work in IE8
obj.class();

You can use them in IE8 if you use the subscript notation:

// this works
obj['class']();

but this contradicts our style guide preference of using dot notation to access properties by name and subscript notation to access properties by variable #properties.

This is another one of those preference decisions where it might sound like "you can't use reserved words as property names" but really what we're trying to say is "we would prefer if you didn't...for now".

Maybe it would be helpful to note what the reserved and future reserved words are and explain that they can be used as Identifiers but not as an IdentifierName ES5 #7.6.1 in [supporting browsers](http://kangax.github.com/es5-compat-table/#Reserved words as property names).

// SyntaxError: unexpected reserved word
function class(){}

@hshoff hshoff closed this in 020fe03 Aug 7, 2013

@jackchen83

This comment has been minimized.

Show comment
Hide comment
@jackchen83

jackchen83 Apr 21, 2014

This is also not supported in IE7, such as
var obj = {
default: 'this is default key',
private: 'OK',
class: 'this is class key'
};
The "default" and "class" key here does not work in IE7/IE8, but not for the "private" key

This is also not supported in IE7, such as
var obj = {
default: 'this is default key',
private: 'OK',
class: 'this is class key'
};
The "default" and "class" key here does not work in IE7/IE8, but not for the "private" key

@Krinkle

This comment has been minimized.

Show comment
Hide comment
@Krinkle

Krinkle Jul 18, 2014

Note that this isn't an "IE bug". It's not supported in old IE because IE's JScript engine correctly follows the ECMAScript 3 specification in this regard; which explicitly defines PropertyName as an Identifier, which is "an IdentifierName that is not a ReservedWord".

This was not by design, but at the time merely to document the status quo of different browsers and to ensure consistency. Other browser's engines had this behaviour as well.

Since property names leave no ambiguity with other operators, this useless restriction was removed in ES5 where PropertyName has been redefined as a plain IdentifierName (https://es5.github.io/#x11.1.5). ES5 is still relatively new (not unlike CSS3 and HTML5) and first implemented in IE10 (almost in IE9), Firefox 4, Safari 6 and Chrome 19.

I'd recommend using something like JSHint (and set option es3: true), and also use it in your editor, to automatically catch these (and other things) ahead of time.

Krinkle commented Jul 18, 2014

Note that this isn't an "IE bug". It's not supported in old IE because IE's JScript engine correctly follows the ECMAScript 3 specification in this regard; which explicitly defines PropertyName as an Identifier, which is "an IdentifierName that is not a ReservedWord".

This was not by design, but at the time merely to document the status quo of different browsers and to ensure consistency. Other browser's engines had this behaviour as well.

Since property names leave no ambiguity with other operators, this useless restriction was removed in ES5 where PropertyName has been redefined as a plain IdentifierName (https://es5.github.io/#x11.1.5). ES5 is still relatively new (not unlike CSS3 and HTML5) and first implemented in IE10 (almost in IE9), Firefox 4, Safari 6 and Chrome 19.

I'd recommend using something like JSHint (and set option es3: true), and also use it in your editor, to automatically catch these (and other things) ahead of time.

@rstacruz

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz May 15, 2015

considering most people use Babel.js now (does Airbnb?), maybe this can be rescinded. It will automatically transpile aoeu.class to aoeu['class']

considering most people use Babel.js now (does Airbnb?), maybe this can be rescinded. It will automatically transpile aoeu.class to aoeu['class']

@goatslacker goatslacker reopened this May 15, 2015

@goatslacker

This comment has been minimized.

Show comment
Hide comment
@goatslacker

goatslacker May 15, 2015

Member

Yeah this can be updated.

Member

goatslacker commented May 15, 2015

Yeah this can be updated.

@supersky07 supersky07 referenced this issue in LuckyBrick/LuckyBrick Dec 14, 2015

Open

[发布]Airbnb JavaScript Style Guide中文版 #5

lencioni added a commit that referenced this issue Aug 3, 2016

Remove rules about reserved words
In 53b4173 we removed the ES5 guide which contains a lot of guidelines
that are no longer very relevant for us. Similarly, some folks have
raised the relevance of these rules about reserved words, given that we
are now living in an age where ES3 support has mostly waned and
transpilers such as Babel are widely adopted and pave over these issues.
This seems like a good opportunity to simplify.

Fixes #61

@ljharb ljharb closed this in #991 Aug 3, 2016

@mqy1023 mqy1023 referenced this issue in mqy1023/react-with-es6 Aug 29, 2016

Closed

【收藏】Airbnb JavaScript Style Guide #3

@muwenzi muwenzi referenced this issue in muwenzi/Program-Blog Oct 26, 2016

Open

ES2015+语言规范 #38

hibearpanda added a commit to 15Prospects/javascript that referenced this issue Jan 22, 2017

Remove rules about reserved words
In 53b4173 we removed the ES5 guide which contains a lot of guidelines
that are no longer very relevant for us. Similarly, some folks have
raised the relevance of these rules about reserved words, given that we
are now living in an age where ES3 support has mostly waned and
transpilers such as Babel are widely adopted and pave over these issues.
This seems like a good opportunity to simplify.

Fixes #61

@bobzhang bobzhang referenced this issue in BuckleScript/bucklescript Feb 2, 2017

Closed

keyword as property #1151

jaylaw81 added a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017

Remove rules about reserved words
In 53b4173 we removed the ES5 guide which contains a lot of guidelines
that are no longer very relevant for us. Similarly, some folks have
raised the relevance of these rules about reserved words, given that we
are now living in an age where ES3 support has mostly waned and
transpilers such as Babel are widely adopted and pave over these issues.
This seems like a good opportunity to simplify.

Fixes #61

@kangning1206 kangning1206 referenced this issue in kangning1206/doc Oct 7, 2017

Open

code 规范 #44

@yanlee26 yanlee26 referenced this issue in yanlee26/yanlee26.github.io Feb 4, 2018

Open

JavaScript Style Guide #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment