我有一段JavaScript代码显示:
function(next, feather) {
var l = Number(171) + (next || 0);
var m = Math.max(1, l - 9);
return {
lc: 300 * (l + 1) * m + (5 * feather || 0)
}
}
Run Code Online (Sandbox Code Playgroud)
现在我已经简化了一点.但任何人都可以解释"|| 0"的作用吗?据我所知,它什么也没做.
(注意我用Number(171)替换了一个函数,因为该函数有效地返回一个数字,羽毛也应该是一个数字,大部分时间是0,有时是1).
我的代码基本上是这样的:
console.log(placeCost) //this returns 0 (the number, not string)
if (!placeCost || placeCost == false || placeCost == "undefined" || placeCost == '') {
console.log("no")
}
else {console.log('yes')}
Run Code Online (Sandbox Code Playgroud)
结果在控制台中为“否”。为什么这会解析为“true”?
假设我有一个这样的函数:
checkerFunction() {
let checker = false;
// Note here I only do a return if the *if* condition is fulfilled
if (condition) {
checker = true;
return checker;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我用它作为另一个函数的真/假检查:
anotherFunction() {
// What will happen here if there's no return from this.checkerFunction()
if (this.checkerFunction()) {
somethingHappens;
}
}
Run Code Online (Sandbox Code Playgroud)
将会发生什么
if (this.checkerFunction())
Run Code Online (Sandbox Code Playgroud)
如果 checkerFunction() 中没有返回,因为条件从来都不是 true...它会被解释为 true/false 还是这将是一个错误,为什么?
!aJavaScript 中的和之间有区别吗a===null?我正在从事一个表现不同的项目。
我不是在谈论布尔值,但!true我不会和true===null
if (!this.props.user)结果为真,
好像(this.props.user === null)结果为 false
我在JavaScript文件中进行了此测试:
if (grouping.conditional(user)) {
console.log(grouping.conditional(user));
}
Run Code Online (Sandbox Code Playgroud)
grouping.conditional 看起来像这样:
conditional: function(user) {
if (user.apilog[0].referer) {
return user.apilog[0].referer.indexOf('.google.')
}
else {
return false
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它-1在某些情况下输出,不在-1 falseJavaScript中?在这种情况下,怎么来的if (grouping.conditional(user))回报true?
我发现了这个地方:而不是if (count != undefined && count != null),使用if(count != null).我可以使用if (count == null)和if(!count)互换?
javascript ×6