javascript(如果给出"undefined")在控制台中 [英] javascript if giving "undefined" in console
问题描述
我确定我只是在俯视某些东西,但看不到它是什么.
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');
返回
a1
a3
foo
undefined
假定typeof(thisNode.S5.download) != 'undefined'
返回true
和lastSku
返回"24536"
和thisNode.S5.download.sku.toString()
返回"24536"
这是不希望的.
我做了一些分解,看来这是问题的初始if语句.
I did some breaking down and it looks like it's the initial if statement that is the problem.
我进入控制台:if (thisNode.L1.download.sku.toString() == lastSku) {}
我得到未定义"
I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {}
i get "undefined"
所以我一步一步检查了
lastSku
返回"24536"
thisNode
返回一个JSON对象. Object {L1: Object, S2: Object, S3: Object, S5: Object}
thisNode
returns a JSON object. Object {L1: Object, S2: Object, S3: Object, S5: Object}
thisNode.L1
返回Object {box: Object, download: Object, cd: Object}
thisNode.L1.download
返回Object {sku: 24354}
thisNode.L1.download.sku
返回24354
thisNode.L1.download.sku.toString()
返回"24354"
thisNode.L1.download.sku.toString() == lastSku
返回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');
返回
bar
yabba
undefined
请注意,我可以将任何JavaScript放入原始的if语句中,但仍然无法定义,因此并不是没有代码可以跳过.
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.
回顾一下,原始代码块似乎永远都不会到达第7行,但是在运行了第一组if
语句后,它看起来确实确实在所有代码之后都保持了运行状态.
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.
推荐答案
我进入控制台:如果(thisNode.L1.download.sku.toString()== lastSku){}我得到未定义"
I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {} i get "undefined"
这完全是预期的行为.
您看到的undefined
(以及在较大的代码示例中看到的最终的undefined
)只是Chrome的JS控制台,输出最后一条语句求值的值,而在JavaScript中,if
语句不要求值.
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.
在控制台上尝试:
console.log('foo')
您将会看到
foo
undefined
foo
由console.log
输出,undefined
是console.log
的返回值.如果您查看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.
参见下文:
这是 REPL 环境.
这篇关于javascript(如果给出"undefined")在控制台中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!