Javascript对象属性引号 [英] Javascript object property quotes
本文介绍了Javascript对象属性引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下对象:
var VariableName = {
firstProperty: 1,
secondProperty: 2
}
我是否必须将对象属性包装在这样的引号中?
Do I have to wrap the object properties in quotes like this?
var VariableName = {
'firstProperty': 1,
'secondProperty': 2
}
这是 JavaScript对象文字中的单引号正确的答案?
推荐答案
不,你不需要这样做。
引用对象键的唯一理由是
The only reasons to quote object-keys are
- 浏览器/ js引擎保留/使用属性名称(例如IE中的class)
- 您有特殊字符或空格在你的钥匙
所以例如
var VariableName = {
"some-prop": 42, // needs quotation because of `-`
"class": 'foobar' // doesn't syntatically require quotes, but it will fail on some IEs
valid: 'yay' // no quotes required
};
这篇关于Javascript对象属性引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文