为什么这个对象属性未定义?

发布于 2024-12-12 04:46:17 字数 982 浏览 8 评论 0原文

考虑下面的代码。第一个 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-12-19 04:46:17

我已经解决了这个问题。基本上,所涉及的对象 (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 function a() 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 the success callback function if a() should be called yet.

After a() runs, function b() is supposed to perform operations on that.data[i].cards. However, b() was running prior to a() being called because of a()'s reliance on the asynchronous requests. So the solution was simply to make a() call b().

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 the cards 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 :)

走野 2024-12-19 04:46:17

我认为对象键具有不可打印的字符,可以像这样复制:

var obj = {};
obj["E"+String.fromCharCode(15)] = new Array(15);

console.log(obj);

/*Object
E: Array[15]
__proto__: Object*/

console.log(obj.E)

//undefined

console.log( obj["E"+String.fromCharCode(15)] )

//[]

编辑:您可以看看您的对象键是否属于这种情况:

var realKeys = [];

for( var key in obj ) {
realKeys.push( [].slice.call( key ).map( function(v){return v.charCodeAt(0);} ).join(" ") );
}

//["69 15"] (69 stands for the letter "E" and 15 was the unprintable character I added manually)

编辑2:因为您不能这样做,所以我想出了另一种方法来查看是否有是不可打印的字符:

像这样复制粘贴密钥字符串:(在两端尽可能多地粘贴,以便选择任何不可见的字符)

然后转储你的像这样的剪贴板(确保使用双引号):

I think the object keys have unprintable characters, such can be replicated like this:

var obj = {};
obj["E"+String.fromCharCode(15)] = new Array(15);

console.log(obj);

/*Object
E: Array[15]
__proto__: Object*/

console.log(obj.E)

//undefined

console.log( obj["E"+String.fromCharCode(15)] )

//[]

Edit: you can see if this is the case for your object keys:

var realKeys = [];

for( var key in obj ) {
realKeys.push( [].slice.call( key ).map( function(v){return v.charCodeAt(0);} ).join(" ") );
}

//["69 15"] (69 stands for the letter "E" and 15 was the unprintable character I added manually)

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):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文