简体   繁体   English

访问javascript对象中包含斜杠的属性名称

[英]Accessing attribute name containing slash in javascript object

Hi I'm trying to access an js object property which has an slash "/" in its name. 嗨,我正在尝试访问名称中带有斜杠“ /”的js对象属性。

The object its somthing like: 该对象的东西像:

{
   my/key : "my value"
   // more stuff here...
}

I try the following construction: 我尝试以下构造:

myObject["my/key"]

If I try to it in Chrome DevTools it works correctly but when I execute my code i get a beautiful undefined on browser console (using console.log()) 如果我尝试在Chrome DevTools中正常运行,但是执行代码时,我会在浏览器控制台上看到一个漂亮的未定义(使用console.log())

has anybody any idea of what's happening? 有人知道发生了什么吗? :S :S

When you enclose the prop name into quotes, it works also in the code: 当您将prop名称括在引号中时,它也可以在代码中使用:

var obj = {
    'my/key' : 'my value'
};

You can check this at jsFiddle . 您可以在jsFiddle上检查。

I tried your code in Node.js and it is working as expected, as long as the property name is quoted. 我在Node.js中尝试了您的代码,只要引用了属性名,它就可以按预期工作。

Try outputting the exact value of the key ("my/key") that you use to access the value - maybe, you are using a different key there. 尝试输出用于访问值的键(“ my / key”)的确切值-也许您正在使用其他键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM