我正在查看http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options来为jQuery创建一个简单的插件.在关于选项和设置的部分之后,我执行了以下操作,但这些操作无效(脚本在遇到设置时退出).
var settings = {
'location' : 'top',
'background-color': 'blue'
}
...
$this.css('backgroundColor', settings.background-color); // fails here
Run Code Online (Sandbox Code Playgroud)
一旦我从背景颜色中删除了短划线,一切正常.
var settings = {
'location' : 'top',
'backgroundColor': 'blue' // dash removed here
}
...
$this.css('backgroundColor', settings.backgroundColor);
Run Code Online (Sandbox Code Playgroud)
我错过了什么,或者jQuery文档错了吗?
Dan*_*ite 110
没有.解析器会将其解释为减法运算符.
你能做到的settings['background-color'].
字符串中可以有破折号。如果您确实想保留该破折号,则必须使用括号之类的内容来引用该属性:
$this.css('backgroundColor', settings['background-color']);
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
var myObject = {
propertyOne: 'Something',
'property-two': 'Something two'
}
for (const val of [
myObject.propertyOne,
myObject['propertyOne'],
myObject['property-two']
]){
console.log(val)
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57233 次 |
| 最近记录: |