我有一些 JSON 通过 jQuery 进行解析并加载到对象 $framework
中。我可以调用 console.log($framework) 并返回:
Object
List item
currentPage: Object
hash: ""
path: "/"
routes: Array[3]
url: "http://example.com.dev/"
__proto__: Object
例如,我可以执行 console.log($framework.path)
,但是 console.log($framework.currentPage)
或 console.log ($framework.routes)
始终返回未定义。在控制台中,我实际上可以展开两者并查看它们的子属性,因此它们似乎确实存在。我做错了什么?
编辑:根据要求,这是我正在使用的路由属性的 JSON:
{
"routes": [
{
"title": "Root",
"path": "/",
"path_plain": "",
"partial": "index",
"container": "#main_content"
},
{
"title": "Content",
"path": "/content",
"path_plain": "content",
"partial": "content/index",
"container": "#main_content"
},
{
"title": "Dashboard",
"path": "/dashboard",
"path_plain": "dashboard",
"partial": "dashboard/index",
"container": "#main_content"
}
]
}
编辑:在进一步解决这个问题之后,事实证明真正的罪魁祸首是在 $framework 完全设置之前发生的自定义回调。我无法解释为什么我可以在直接引用属性之前和之后在控制台中看到对象并且仍然未定义,但至少它已经解决了。
最佳答案
试试这个:console.log($framework.routes[0])
来记录第一个项目,console.log($framework.routes[1])
如果您想查看内容,则可以选择第二个,依此类推。
根据您的 JSON,$framework.routes[0].title
应返回 Root
。
关于javascript - 子对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918488/