为什么这个对象属性未定义?
考虑下面的代码。第一个 console.log
正确记录了图像,您可以在下图中看到其属性。然而,当我尝试将其属性记录到控制台时,我得到了未定义
!
console.log(that.data[0].cards); //works -- see image below
console.log(that.data[0].cards.E); //undefined
console.log(that.data[0].cards['E']); //undefined
console.log(that.data[0].cards.hasOwnProperty('E')); //false
var test = JSON.stringify(that.data[0]);
console.log(test); // {}
for( var key in that.data[0].cards ) {
console.log('hello????') //doesn't appear in the console
}
console.log( Object.keys( that.data[0].cards ) ); //[]
console.log( that.data[0].cards.propertyIsEnumerable("E") ); //false
console.log( that.data[0].cards.__lookupGetter__( "E" ) ); //undefined
控制台中的结果:
知道这里发生了什么吗? that.data[0]
内部的 xml
属性内部也应该有属性——事实上,与 cards< 中的属性命名相同。 /代码>。
FWIW,我在 Firebug 中得到了同样的结果(上面的控制台图像是 Chrome)。
Consider the code below. The first console.log
correctly logs the image, and you can see its properties in the image below. However, when I try logging one if its properties to the console, I get undefined
!
console.log(that.data[0].cards); //works -- see image below
console.log(that.data[0].cards.E); //undefined
console.log(that.data[0].cards['E']); //undefined
console.log(that.data[0].cards.hasOwnProperty('E')); //false
var test = JSON.stringify(that.data[0]);
console.log(test); // {}
for( var key in that.data[0].cards ) {
console.log('hello????') //doesn't appear in the console
}
console.log( Object.keys( that.data[0].cards ) ); //[]
console.log( that.data[0].cards.propertyIsEnumerable("E") ); //false
console.log( that.data[0].cards.__lookupGetter__( "E" ) ); //undefined
The result in the console:
Any idea what's going on here? The xml
property inside of that.data[0]
should also have properties inside of it -- named the same, in fact, as the properties in cards
.
FWIW, I get the same thing in Firebug (the above console image is Chrome).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经解决了这个问题。基本上,所涉及的对象 (
that.data[0].cards
) 的属性是由函数a()
创建的,该函数在所有 AJAX 请求之后运行以获取必要的信息。 XML 文件已被处理。我允许请求异步运行,使用计数器在success
回调函数中确定是否应该调用a()
。在
a()
运行后,函数b()
应该对that.data[i].cards
执行操作。但是,由于a()
依赖于异步请求,因此b()
在调用a()
之前运行。因此,解决方案很简单,让a()
调用b()
。所以这对我来说是一个非常简单的错误。令人困惑的是,将
that.data[0].cards
记录到控制台向我表明,事实上cards
对象已经构建,当事实上还没有。所以控制台向我提供了不正确的——或者至少是不清楚的——信息。感谢昨晚大家的帮助!大家都点赞:)
I've solved the problem. Basically, the object in question (
that.data[0].cards
) has its properties created by a functiona()
that runs after all the AJAX requests for the necessary XML files have been processed. I allow the requests to run asynchronously, using a counter to determine in thesuccess
callback function ifa()
should be called yet.After
a()
runs, functionb()
is supposed to perform operations onthat.data[i].cards
. However,b()
was running prior toa()
being called because ofa()
's reliance on the asynchronous requests. So the solution was simply to makea()
callb()
.So this turned out to be a pretty simple mistake on my part. What made it so confusing was the fact that logging
that.data[0].cards
to the console showed me that in fact thecards
object had already been built, when in fact it had not yet. So the console was providing me with incorrect--or at least unclear--information.Thanks for everyone's help last night! Upvotes all around :)
我认为对象键具有不可打印的字符,可以像这样复制:
编辑:您可以看看您的对象键是否属于这种情况:
编辑2:因为您不能这样做,所以我想出了另一种方法来查看是否有是不可打印的字符:
像这样复制粘贴密钥字符串:(在两端尽可能多地粘贴,以便选择任何不可见的字符)
然后转储你的像这样的剪贴板(确保使用双引号):
I think the object keys have unprintable characters, such can be replicated like this:
Edit: you can see if this is the case for your object keys:
Edit2: Since you can't do that I came up with another way to see if there are unprintable characters:
Copypaste the key string like this: (go all the way as much as you can on both ends so you pick any invisible characters)
Then dump your clipboard like this (Make sure you are using double quotes):