Javascript 使用保留字作为属性名称,重新审视
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7022397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using reserved words as property names, revisited
提问by cc young
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 reserved word as a property name in JavaScript. The answer seemed general consensus by Alex Wayne:
这个问题是在之前的 Stack Overflow 问题中间接提出的:Browser support for using a reserved word as a property name in JavaScript。答案似乎是Alex Wayne 的普遍共识:
You can use those words, but only as strings and not shorthand properties.
foo['class']; // cool foo.class; // not cool
您可以使用这些词,但只能用作字符串而不是速记属性。
foo['class']; // cool foo.class; // not cool
While I think that they are probably more knowledgeable than me in this area and it is probably a bad ideato use reserved words in some situations, I think their conclusion is wrong based on two points:
虽然我认为他们在这方面可能比我更了解,并且在某些情况下使用保留字可能是一个坏主意,但我认为他们的结论基于两点是错误的:
testing of the reserved words using them as a "shorthand" properties
the HTMLFormElement makes it impossible notto use reserved words in "shorthand"
使用它们作为“速记”属性测试保留字
HTMLFormElement 使得在“速记”中不使用保留字成为可能
First, using the reserved word list, each was added as a property to an Object
and HTMLElement
, both as obj["word"]
and obj.word
, and then retrieved as obj["word"]
and obj.word
. In each of the 63 cases all eight tests worked correctly.
首先,使用保留字列表,将每个作为属性添加到Object
and 中HTMLElement
,都作为obj["word"]
and obj.word
,然后作为obj["word"]
and检索obj.word
。在 63 个案例中的每一个案例中,所有八项测试都正常工作。
Second, the HTMLFormElement necessitates this works because it retrieves in its elements using shorthand notation. If <input name='typeof' value='scalar' />
is an element of a form, then form.typeof
== "scalar".
其次,HTMLFormElement 需要这样做,因为它使用速记符号检索其元素。如果<input name='typeof' value='scalar' />
是表单元素,则form.typeof
==“标量”。
From my experience, reserved words are usually data inflicted (e.g. a column named "private"), not program inflicted. As such they contaminate JSON objects, and from there input, and from there the HTMLFormElement. Simply put, without a huge amount of (IMHO unnecessary) work, it's impossible to keep reserved words notbeing forced to work correctly in shorthand.
根据我的经验,保留字通常是数据造成的(例如名为“private”的列),而不是程序造成的。因此,它们会污染 JSON 对象,并从那里污染输入,并从那里污染 HTMLFormElement。简而言之,如果没有大量(恕我直言不必要的)工作,就不可能保持保留字不被强制在速记中正确工作。
It seems to me these real problems:
在我看来,这些真正的问题是:
care needs to be taken not to conflict with existent properties, not reserved words
(many if not all) variablescannot be reserved words
use of reserved words as properties can be (but are not necessarily) confusing
需要注意不要与现有属性冲突,而不是保留字
(许多如果不是全部)变量不能是保留字
使用保留字作为属性可能(但不一定)令人困惑
Is this conclusion correct then, that reserved words as property names, and accessing them either as strings or shorthand, is just fine - as long as a little common sense is applied to the situation?
那么这个结论是否正确,保留字作为属性名称,并作为字符串或速记访问它们,就可以了 - 只要对这种情况应用一点常识?
回答by UIZE
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.
在 ECMAScript 中,从 ES5 开始,保留字可以用作“buff”中的对象属性名称。这意味着在定义对象字面量时不需要用引号“包裹”它们,并且可以在对象上取消引用(用于访问、分配和删除),而不必使用方括号索引符号。
That said, reserved words may still NOTbe 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
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:
以下是关键字,不能用作变量、函数、方法或对象标识符,因为 ECMAScript 为它们指定了特殊的行为:
回答by RoToRa
I'm not quite sure what the point is you want to make, so the only answer I can give is: Yes, it's ok to use reserved words as property names.
我不太确定你想表达什么,所以我能给出的唯一答案是:是的,可以使用保留字作为属性名称。
(However two small remarks: foo["class"]
is ok, not foo[class]
. And any way you should be using form.elements["xyz"]
and not form.xyz
to access an element named xyz
.)
(但是有两个小说明:foo["class"]
可以,不是foo[class]
。以及您应该使用的任何方式, form.elements["xyz"]
而不是form.xyz
访问名为 的元素xyz
。)
回答by gumkins
Yes, it can be used.
是的,它可以使用。
Just small remark, if you use YUI compressor you have to put property name which is equal to one of js reserved words in quotes.
只是小说一下,如果您使用 YUI 压缩器,则必须将等于 js 保留字之一的属性名称放在引号中。
For example, this won't compress
例如,这不会压缩
var a = { case : "foo"}; // syntax error, "invalid property id"
a.for = "bar"; // syntax error, "missing name after . operator"
This will do
这会做
var a = { "case" : "foo"}; //OK
a["for"] = "bar"; //OK
Here is Online JavaScript/CSS Compression Using YUI Compressorwhere this can be tested.
这是使用 YUI Compressor 的在线 JavaScript/CSS 压缩,可以在其中进行测试。
回答by David Gilbertson
Yes, in most browsers (including IE9+)
是的,在大多数浏览器中(包括 IE9+)
There's actually an entry in the Kangax compatibility table for "Reserved words as property names"
Kangax 兼容性表中实际上有一个条目“保留词作为属性名称”