是否允许在javascript属性名称中使用破折号? [英] Are dashes allowed in javascript property names?

查看:417
本文介绍了是否允许在javascript属性名称中使用破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options 为jQuery创建一个简单的插件。在关于选项和设置的部分之后,我执行了以下操作,这些操作无效(脚本在遇到设置时退出)。

I was looking at http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options to create a simple plugin for jQuery. Following the section about options and settings, I did the following, which didn't work (the script quit when it encountered the setting).

var settings = {
    'location' : 'top',
    'background-color': 'blue'
}
...
$this.css('backgroundColor', settings.background-color); // fails here

一旦我从背景颜色中删除了短划线,一切正常。

Once I removed the dash from the background color, things work properly.

var settings = {
    'location' : 'top',
    'backgroundColor': 'blue' // dash removed here
}
...
$this.css('backgroundColor', settings.backgroundColor); 

我错过了什么,或者jQuery文档错了吗?

Am I missing something, or are the jQuery docs wrong?

推荐答案

没有。解析器会将其解释为减法运算符。

no. the parser will interpret it as the subtract operator.

你可以做 settings ['background-color']

这篇关于是否允许在javascript属性名称中使用破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆