admin管理员组文章数量:1194371
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).
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).
Share Improve this question edited Oct 27, 2011 at 21:31 maxedison asked Oct 27, 2011 at 20:51 maxedisonmaxedison 17.6k15 gold badges70 silver badges118 bronze badges 5 |2 Answers
Reset to default 14I'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 :)
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):
本文标签: javascriptWhy is this object property undefinedStack Overflow
版权声明:本文标题:javascript - Why is this object property undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738493713a2089857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
JSON.stringify(that)
doesn't seem to do anything except bring the script to a complete stop. Doesn't throw an error or anying else, but nothing runs after the call toJSON.stringify(that)
. I've updated my question to show the results ofJSON.stringify(that.data[0].cards)
, which just shows it to be a blank object{}
. – maxedison Commented Oct 27, 2011 at 21:15