Using reserved words as property names, revisited

后端 未结 4 1285
臣服心动
臣服心动 2020-11-28 09:27

Can a reserved word be used as an object\'s property name?

This issue was raised indirectly in a previous Stack Overflow question: Browser support for using a reserv

4条回答
  •  佛祖请我去吃肉
    2020-11-28 10:26

    In ECMAScript, starting from ES5, reserved words may be used as object property names "in the buff". This means that they don't need to be "clothed" in quotes when defining object literals, and they can be dereferenced (for accessing, assigning, and deleting) on objects without having to use square bracket indexing notation.

    That said, reserved words may still NOT be used as identifier names. This is stated quite unambiguously in the spec and is stated somewhat emphatically here (if you don't want your eyes to bleed by having to read the actual language spec)...

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

    The following are keywords and may not be used as variables, functions, methods, or object identifiers, because ECMAScript specifies special behavior for them:

提交回复
热议问题