javascript 对象属性末尾的破折号 [英] Dash on the end of javascript object property

查看:58
本文介绍了javascript 对象属性末尾的破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以像这样在 javascript 对象属性名称的末尾使用破折号吗?我在任何文档中都没有发现这是无效的,但是在这种情况下尝试访问值 myProp- 时我得到了一些奇怪的结果.

can i use dash on the end of javascript object property name like this. I could not find in any documentation that this is not valid but i got some strange results when trying to access value myProp- in this case.

var myObject = {"myProp-":"myValue"};

我只能像这样访问这个值 myObject["myProp-"] 我想像这样访问

i can only access to this value like this myObject["myProp-"] and i would like to access like

myObject.myProp-

但是我得到了语法错误:意外的令牌}"

but i got " SyntaxError: Unexpected token } "

推荐答案

您必须使用括号表示法而不是点表示法:

You'll have to use bracket notation instead of dot notation:

myObject["myProp-"]

这篇关于javascript 对象属性末尾的破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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