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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7922226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:43:47  来源:igfitidea点击:

Why is this object property undefined?

javascriptobjectundefinedobject-properties

提问by maxedison

Consider the code below. The first console.logcorrectly 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正确记录图像,您可以在下图中看到其属性。但是,当我尝试将其属性记录到控制台时,我得到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

enter image description here

Any idea what's going on here? The xmlproperty inside of that.data[0]should also have properties inside of it -- named the same, in fact, as the properties in cards.

知道这里发生了什么吗?该xml物业内that.data[0]也应该有它的内部性能-命名一样,其实,在属性cards

FWIW, I get the same thing in Firebug (the above console image is Chrome).

FWIW,我在 Firebug 中得到了同样的东西(上面的控制台图像是 Chrome)。

回答by maxedison

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 successcallback function if a()should be called yet.

我已经解决了这个问题。基本上,所讨论的对象 ( that.data[0].cards) 的属性是由一个函数创建的,该函数a()在处理完对必需 XML 文件的所有 AJAX 请求之后运行。我允许请求异步运行,使用计数器在success回调函数中确定是否a()应该被调用。

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().

之后a()运行,功能b()应该执行的操作that.data[i].cards。但是,由于依赖于异步请求,b()因此在a()被调用之前正在运行a()。所以解决方案就是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].cardsto the console showed me that in fact the cardsobject had already been built, when in fact it had not yet. So the console was providing me with incorrect--or at least unclear--information.

所以这对我来说是一个非常简单的错误。令人如此困惑的是,登录that.data[0].cards控制台向我显示该cards对象实际上已经构建,而实际上尚未构建。所以控制台向我提供了不正确的——或者至少是不清楚的——信息。

Thanks for everyone's help last night! Upvotes all around :)

谢谢大家昨晚的帮助!到处点赞:)

回答by Esailija

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:

Edit2:既然你不能这样做,我想出了另一种方法来查看是否有不可打印的字符:

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

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