使用 $ 作为 JS 对象中的键?
示例:
console.log(myobject['media$group']);
输出未定义
console.log(myobject['media\$group']);
输出期望值
这是怎么回事?我在使用 youtube 的 API 时遇到了这个问题。
编辑:
我对你们很多人说它有效感到困惑,所以我会发布更多信息,因为也许我错过了一些愚蠢的东西。
这是我正在运行的代码:
console.log(feed);
console.log(feed["media$group"]["media$thumbnail"]);
这是从 firebug 剪切\粘贴的第一个日志中的对象:
author
[Object { name={...}, uri={...}}]
category
[Object { scheme="http://schemas.google.com/g/2005#kind", term="http://gdata.youtube.com/schemas/2007#video"}, Object { scheme="http://gdata.youtube.co...mas/2007/categories.cat", term="Tech", label="Science & Technology"}, Object { scheme="http://gdata.youtube.com/schemas/2007/keywords.cat", term="google"}, 10 more...]
content
Object { $t="The Google Doodle team ...googleblog.blogspot.com", type="text"}
gd$comments
Object { gd$feedLink={...}}
gd$rating
Object { average=4.77813, max=5, more...}
id
Object { $t="http://gdata.youtube.co.../api/videos/3NGSU2PM9dA"}
link
[Object { rel="alternate", type="text/html", href="http://www.youtube.com/...A&feature=youtube_gdata"}, Object { rel="http://gdata.youtube.co...as/2007#video.responses", type="application/atom+xml", href="http://gdata.youtube.co...s/3NGSU2PM9dA/responses"}, Object { rel="http://gdata.youtube.co...emas/2007#video.related", type="application/atom+xml", href="http://gdata.youtube.co...eos/3NGSU2PM9dA/related"}, 2 more...]
media$group
Object { media$category=[1], media$content=[3], more...}
published
Object { $t="2011-04-14T00:31:33.000Z"}
title
Object { $t="Charlie Chaplin Google Doodle", type="text"}
updated
Object { $t="2011-04-15T23:14:58.000Z"}
yt$statistics
Object { favoriteCount="2539", viewCount="0"}
这是第二个 console.log 中的错误:
feed.media is undefined
[Break On This Error] console.log(feed["media"]["media"]);
Example:
console.log(myobject['media$group']);
outputs undefined
console.log(myobject['media\$group']);
outputs expected value
What's the deal? I ran into this when using youtube's API.
EDIT:
I'm confused by many of you saying it works, so I will post more info because perhaps I'm missing something stupid.
Here's the code I'm running:
console.log(feed);
console.log(feed["media$group"]["media$thumbnail"]);
Here's the object from the first log cut\pasted from firebug:
author
[Object { name={...}, uri={...}}]
category
[Object { scheme="http://schemas.google.com/g/2005#kind", term="http://gdata.youtube.com/schemas/2007#video"}, Object { scheme="http://gdata.youtube.co...mas/2007/categories.cat", term="Tech", label="Science & Technology"}, Object { scheme="http://gdata.youtube.com/schemas/2007/keywords.cat", term="google"}, 10 more...]
content
Object { $t="The Google Doodle team ...googleblog.blogspot.com", type="text"}
gd$comments
Object { gd$feedLink={...}}
gd$rating
Object { average=4.77813, max=5, more...}
id
Object { $t="http://gdata.youtube.co.../api/videos/3NGSU2PM9dA"}
link
[Object { rel="alternate", type="text/html", href="http://www.youtube.com/...A&feature=youtube_gdata"}, Object { rel="http://gdata.youtube.co...as/2007#video.responses", type="application/atom+xml", href="http://gdata.youtube.co...s/3NGSU2PM9dA/responses"}, Object { rel="http://gdata.youtube.co...emas/2007#video.related", type="application/atom+xml", href="http://gdata.youtube.co...eos/3NGSU2PM9dA/related"}, 2 more...]
media$group
Object { media$category=[1], media$content=[3], more...}
published
Object { $t="2011-04-14T00:31:33.000Z"}
title
Object { $t="Charlie Chaplin Google Doodle", type="text"}
updated
Object { $t="2011-04-15T23:14:58.000Z"}
yt$statistics
Object { favoriteCount="2539", viewCount="0"}
And here's the error from the second console.log:
feed.media is undefined
[Break On This Error] console.log(feed["media"]["media"]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
'media\$group'
和'media$group'
相等,因为\$
不是已知的转义序列,因此会变成$。
因此,该字段只是在两次调用之间定义的。
'media\$group'
and'media$group'
are equal since\$
is not a known escape sequence and thus turns into$
.So the field simply got defined between your two calls.
你的情况很有趣,我不知道为什么你需要用反斜杠转义 $ 字符,但这些是预期的输出。
Your case is interesting, I don't know why you required to escape $ character with a back slash, but these are outputs as expected.
听起来像是服务器转义了字符串中的字符。该或其他一些字符串构建系统已转义
$
字符,然后将您的对象设置为该值。还有
"foo$bar" === "foo\$bar"
Sounds like the server escaping characters in your string. That or some other string building system has been escaping the
$
character and then setting your object onto that value.Also
"foo$bar" === "foo\$bar"
效果很好。
尝试这个 (jsfiddle)
It works fine.
Try this (jsfiddle)