有效的 javascript 对象属性名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2940424/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:36:08  来源:igfitidea点击:

Valid javascript object property names

javascript

提问by hawkett

I'm trying to work out what is considered valid for the property name of a javascript object. For example

我正在尝试找出对 javascript 对象的属性名称有效的内容。例如

var b = {}
b['-^colour'] = "blue";     // Works fine in Firefox, Chrome, Safari
b['colour'] = "green";      // Ditto
alert(b['-^colour']);       // Ditto
alert(b.colour);            // Ditto
for(prop in b) alert(prop); // Ditto
//alert(b.-^colour);     // Fails (expected)

This postdetails valid javascript variable names, and '-^colour' is clearly not valid (as a variable name). Does the same apply to object property names? Looking at the above I'm trying to work out if

这篇文章详细介绍了有效的 javascript 变量名,并且 '-^colour' 显然是无效的(作为变量名)。这同样适用于对象属性名称吗?看看上面的我正在努力解决如果

  1. b['-^colour'] is invalid, but works in all browsers by quirk, and I shouldn't trust it to work going forward

  2. b['-^colour'] is completely valid, but it's just of a form that can only be accessed in this manner - (it's supported so Objects can be used as maps perhaps?)

  3. Something else

  1. b['-^colour'] 无效,但通过 quirk 在所有浏览器中都有效,我不应该相信它会继续工作

  2. b['-^colour'] 是完全有效的,但它只是一种只能以这种方式访问​​的形式 - (它被支持所以对象可以用作地图?)

  3. 别的东西

As an aside, a global variable in javascript might be declared at the top level as

顺便说一句,javascript中的全局变量可能会在顶层声明为

var abc = 0;

but could also be created (as I understand it) with

但也可以创建(据我所知)

window['abc'] = 0;

the following works in all the above browsers

以下适用于所有上述浏览器

window['@£$%'] = "bling!";
alert(window['@£$%']);

Is this valid? It seems to contradict the variable naming rules - or am I not declaring a variable there? What's the difference between a variable and an object property name?

这是有效的吗?这似乎与变量命名规则相矛盾 - 或者我没有在那里声明变量?变量名和对象属性名有什么区别?

采纳答案by Matthew Flaschen

Yes, objects can be used as maps, and any string can be a property name. As you've discovered, some properties can onlybe accessed using the bracket syntax.

是的,对象可以用作地图,任何字符串都可以是属性名称。正如您所发现的,某些属性只能使用括号语法访问。

window['abc']

is accessing a property. It is not a variable, even though it refers to the same value (at the global level) as:

正在访问一个属性。它不是一个变量,即使它指的是与以下相同的值(在全局级别):

abc

回答by Matti Virkkunen

Object property naming rules and variable naming rules are separate. The standard only "reserves" a handful of property names (such as prototypeand constructor, IIRC), but other than those, any string goes.

对象属性命名规则和变量命名规则是分开的。该标准仅“保留”了少数属性名称(例如prototypeand constructor、IIRC),但除此之外,任何字符串都可以使用。

Except when the execution environment (i.e. the browser) decides to add more magic properties, of course. (I hear setting __proto__breaks some things in quite weird ways)

当然,除非执行环境(即浏览器)决定添加更多魔法属性。(我听说设置__proto__以非常奇怪的方式破坏了一些东西)

回答by ytropek

  1. Every time you create a global variable you create in fact a new member of a global object(which is windowin browser environment, globalin Node.js, etc.). This is why window.xis exactly the same like (global) var x, this.xor just x.

  2. Understanding JavaScript object like a mapis quite right, because: a) you can add a new element dynamically - at any moment; b) the element can have any name - also including special characters, c) you can try to access a non-existing element of an object/map and it is not an error, d) you can remove an element from an object.

  3. If you like to access object members with standard dot notation(eg. a.x) you are not allowed to use any special characters different than _ or $; also the name cannot start from a number. For all other cases you are forced to use square brackets and quotation marks to access object elements.

  1. 每次创建全局变量时,实际上都会创建全局对象的一个新成员(window在浏览器环境中,global在 Node.js 中,等等)。这就是为什么window.x和 (global) 完全一样var xthis.x或者只是x.

  2. 像地图一样理解 JavaScript对象是非常正确的,因为: a) 您可以随时动态添加新元素;b) 元素可以有任何名称——也包括特殊字符,c) 您可以尝试访问对象/映射的不存在元素,这不是错误,d) 您可以从对象中删除元素。

  3. 如果您喜欢使用标准点符号(例如a.x)访问对象成员,则不允许使用任何不同于 _ 或 $ 的特殊字符;名称也不能以数字开头。对于所有其他情况,您必须使用方括号和引号来访问对象元素。