[英]Question about Javascript Shorthand object quotes in the key
I'm pretty new to javascript and am wondering about quoting the keys in object shorthand 我是javascript的新手,我想知道在对象速记中引用键
So I am using the OpenLayers js library and many of the object constructors take {options}
as the argument for setting different variables, callbacks, etc. 所以我使用OpenLayers js库,许多对象构造函数将{options}
作为设置不同变量,回调等的参数。
In my code I have a bunch of control objects which are used to manipulate the map and what-not 在我的代码中,我有一堆控件对象,用于操作地图和什么不是
controls = {
navigation : new OpenLayers.Control.Navigation({'autoActivate' : false}),
zoom_out_box : new OpenLayers.Control.ZoomBox({
alwaysZoom : true,
out : true
}),
...
};
In some of their examples they use single quotes for the keys and others they won't {'ascending':false}
or {visibility: false}
. 在他们的一些示例中,他们使用单引号作为键,而其他人则不会{'ascending':false}
或{visibility: false}
。
I thought that maybe it had to do with reserved words or functions vs variables but I can add functions to my zoom box: 我认为这可能与保留字或函数与变量有关,但我可以在缩放框中添加函数:
controls= {
zoom_out_box : new OpenLayers.Control.ZoomBox({
if : function(e){alert('blah');}
}),
zoom_out_box_2 : new OpenLayers.Control.ZoomBox({
'if' : function(e){alert('blah');}
})
};
I test it with an onlclick="controls.zoom_out_box.if(this)"
and it alerts fine and I get no warnings or errors in firebug. 我使用onlclick="controls.zoom_out_box.if(this)"
对其进行测试,并且它提示良好,我在firebug中没有任何警告或错误。
So whats the difference in the quoting? 那么报价的差异是什么?
You can use non-identifiers as keys in a JavaScript object, in which case, the quotes are required: 您可以在JavaScript对象中使用非标识符作为键,在这种情况下,引号是必需的:
var obj = { 'not-an-identifier': 42 };
In the case where an identifier is quoted, it's just a matter of style/preference/convention. 在引用标识符的情况下,它只是样式/偏好/约定的问题。
As a side note, non-identifiers must always be accessed using the square-bracket/array-style notation ( obj['not-an-identifier']
), rather than the dot ( .
) notation. 作为旁注,必须始终使用方括号/数组样式表示法( obj['not-an-identifier']
)而不是点( .
)表示法来访问非标识符。
The currently accepted answer is incorrect: 目前接受的答案是错误的:
You can use non-identifiers as keys in a JavaScript object, in which case, the quotes are required 您可以在JavaScript对象中使用非标识符作为键,在这种情况下,引号是必需的
The quotes aren't required if you use a numeric literal as a property name. 如果使用数字文字作为属性名称,则不需要引号。
From Unquoted property names / object keys in JavaScript , my write-up on the subject: 从JavaScript中的Unquoted属性名称/对象键 ,我对该主题的描述:
Quotes can only be omitted if the property name is a numeric literal or a valid identifier name . 如果属性名称是数字文字或有效标识符名称,则只能省略引号。
[…] [...]
Bracket notation can safely be used for all property names. 括号表示法可以安全地用于所有属性名称。
[…] [...]
Dot notation can only be used when the property name is a valid identifier name. 只有当属性名称是有效的标识符名称时,才能使用点表示法。
I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. 我还制作了一个工具,告诉您是否可以使用任何给定的属性名称而不带引号和/或带点符号。 Try it at mothereff.in/js-properties . 在mothereff.in/js-properties试试吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.