JS Promises / A +的'catch'方法名称是否无效,因为它是JS关键字? [英] Is the 'catch' method name of JS Promises/A+ invalid since it's a JS keyword?

查看:196
本文介绍了JS Promises / A +的'catch'方法名称是否无效,因为它是JS关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一个项目中开始使用JS Promises。我注意到每次使用 .catch 我的 JS linter 都会抱怨。它确实运行并做了应有的事情,但我查阅了 ECMAScript规范和看起来确实如此:自 catch 是关键字,不能用作标识符。据我所知,方法名称是标识符,所以这是无效的:

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);

应该是这样的:

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

我缺少什么?

推荐答案


我缺少什么?

What am I missing?

属性名称不是标识符,它可以使用任何标识符名称。根据 Property Accessors 的规范:

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

标识符

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.

然而,这确实随着ES5而改变,回到 EcmaScript 3 属性名称必须是标识符。这就是为什么你仍然需要为关键字使用括号表示法,如果你想支持旧版浏览器;这就是为什么你的linter抱怨它的原因。 同样保留对象文字中的属性名称。

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.

这篇关于JS Promises / A +的'catch'方法名称是否无效,因为它是JS关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆