8

I started to use JS Promises in a project recently. I noticed that every time I use .catch my JS linter complains. It does run and does what it should but I looked up the ECMAScript spec and it really looks like it is right: Since catch is a keyword it can not be used as an identifier. As I understand method names are identifiers, so this is invalid:

Promise.reject("Duh").catch(alert);

It should be this instead:

Promise.reject("Duh")['catch'](alert);

What am I missing?

Fozi
  • 4,973
  • 1
  • 32
  • 56
  • browsers that don't follow the ecmaScript spec might feel differently than those that do. – dandavis Sep 10 '14 at 20:45
  • 2
    The linter likely complains because it tries to be ES3 compatible. – Felix Kling Sep 10 '14 at 20:46
  • 2
    Related: [Browser support for using a reserved word as a property name in JavaScript](http://stackoverflow.com/q/5306315/218196) ... Kangax: http://kangax.github.io/compat-table/es5/#Reserved_words_as_property_names – Felix Kling Sep 10 '14 at 20:49

1 Answers1

18

What am I missing?

A property name is not an identifier, it can use any identifier name. From the spec on Property Accessors:

MemberExpression : MemberExpression . IdentifierName
CallExpression : CallExpression . IdentifierName

and identifiers:

Identifier :: IdentifierName but not ReservedWord

You can use any arbitrary identifer name (but not things like integers) in a dot property access, but you can't use those that are [reserved] keywords as identifier, e.g. in a variable or function name.

However, this did change with ES5, back in EcmaScript 3 property names were required to be identiers. That's why you still need to use the bracket notation for keywords if you want to support legacy browsers; and it's the reason why your linter complains about it. Same holds for property names in object literals.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Not sure why it was downvoted, it's a correct answer. +1 – zerkms Sep 10 '14 at 20:40
  • 1
    well, it corrects a semantics issue with the question (about the word "identifier"), but it doesn't answer about why the warning or about IE8. (but i didn't downvote) – dandavis Sep 10 '14 at 20:41
  • 1
    I downvoted because the original answer was a very poor, short answer that doesn't provide a lot of information. Removed the downvote though as subsequent edits were a lot better. – Evert Sep 10 '14 at 20:42
  • You are right. The key sentence is "An Identifier is an IdentifierName that is not a ReservedWord". +1 – Fozi Sep 10 '14 at 20:47
  • 2
    @Evert: Sorry, [you're right, and I'm ashamed of using this tactic](http://meta.stackexchange.com/q/9731/183280) - only it just works too well. My intention was never to give an incomplete answer, though. – Bergi Sep 10 '14 at 20:52
  • @Bergi: No need to say sorry to me ;) It seems like a bit of a waste of time to me to do this for virtual internet points, but I guess I simply have other means to entertain myself ;) – Evert Sep 11 '14 at 11:58