问题
I'm sure I'm just overlooking something, but I can't see what it is.
I have a page I'm using to test some new code with using the console. Most of it works.
if(typeof(thisNode.L1.download) != 'undefined') {
console.log('a1');
if (thisNode.L1.download.sku.toString() == lastSku) {
console.log('a2');
addSku = thisNode.L1.cd.sku.toString();
} else { console.log('a3'); }
} else if(typeof(thisNode.S5.download) != 'undefined') {
console.log('b1');
if (thisNode.S5.download.sku.toString() == lastSku) {
console.log('b2');
addSku = thisNode.S5.cd.sku.toString();
} else {
console.log('b3');
}
}
console.log('foo');
returns
a1
a3
foo
undefined
given that typeof(thisNode.S5.download) != 'undefined'
returns true
and lastSku
returns "24536"
and thisNode.S5.download.sku.toString()
returns "24536"
This is not expected.
I did some breaking down and it looks like it's the initial if statement that is the problem.
I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {}
i get "undefined"
So I checked it piece by piece
lastSku
returns "24536"
thisNode
returns a JSON object. Object {L1: Object, S2: Object, S3: Object, S5: Object}
thisNode.L1
returns Object {box: Object, download: Object, cd: Object}
thisNode.L1.download
returns Object {sku: 24354}
thisNode.L1.download.sku
returns 24354
thisNode.L1.download.sku.toString()
returns "24354"
thisNode.L1.download.sku.toString() == lastSku
returns false
if (thisNode.L1.download.sku.toString() == lastSku) {
console.log('foo');
} else {
console.log('bar');
}
returns "bar" undefined
if (thisNode.L1.download.sku.toString() == lastSku) {
console.log('foo');
} else {
console.log('bar');
}
console.log('yabba');
returns
bar
yabba
undefined
Note that I can put any JavaScript in the original if statement and i still get undefined, so it's not that there's no code for it to skip.
To recap, the original block doesn't appear to ever get to line 7 but it does look like after running through the first set of if
statements it does keep running code after all of them.
回答1:
I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {} i get "undefined"
That's entirely expected behaviour.
The undefined
you're seeing (and the final undefined
you're seeing in your larger code examples) is just Chrome's JS console outputting the value the last statement evaluates to, and in JavaScript, if
statements don't evaluate to a value.
Try this on the console:
console.log('foo')
You'll see
foo
undefined
The foo
is output by console.log
, the undefined
is the return value of console.log
. If you look to the left of undefined
, you'll see a grey <-
arrow, indicating this is the return value of the last statement, not output caused by the code.
See below:

This is typical behaviour for REPL environments.
回答2:
the undefined
was a red herring I couldn't stop looking at, which meager helpfully got me to finally see.
The real problem was the external if..else if...else if structure. The reason it never checked thisNode.S5
was because I used
if(typeof(thisNode.L1.download) != 'undefined') {
if (thisNode.L1.download.sku.toString() == lastSku) {
//do stuff
}
else if(typeof(thisNode.S5.download) != 'undefined') {
if (thisNode.S5.download.sku.toString() == lastSku) {
//do different stuff
}
}
putting the second check INSIDE the first if
was a dumb movee. I wasn't thinking. I should, instead, have used
if(typeof(thisNode.L1.download) != 'undefined' && thisNode.L1.download.sku.toString() == lastSku) {
//do stuff
} else if(typeof(thisNode.S3.download) != 'undefined' && thisNode.S5.download.sku.toString() == lastSku) {
//do different stuff
}
来源:https://stackoverflow.com/questions/21206931/javascript-if-giving-undefined-in-console