对这里发生的事情有点困惑:
var first = true;
var third = false;
if(first === true || !second || third === false) {
alert('if');
}
else{
alert('else');
}
其他语句每次在这里计算,我很困惑为什么,首先是真,没有变量叫做第二和第三也是假的,只有一个必须是真才能触发if条件,对吗?
缩小之后,我将面临JavaScript的一个问题。相同的代码正在以不同的方式执行。在缩小规模之前,一切都如期而至。我无法理解为什么会发生这种情况。
先于小型化
var a = boolAConditionResult();
var b = boolBConditionResult();
if (a && b) {
doSomething();
}
小型化后
a && b && doSomething();
我知道只有当a和b为true时才会执行doSomething(),但是即使a是false (b为true),doSomethin
给定以下代码:
if ("string") {
console.log('true!');
}
//logs "true" to the console
if ("string"==true) {
console.log('true!');
}
//doesn't log anything
这一切为什么要发生?我认为"string"被强制转换为一个数字,布尔值也是如此。所以true变成了1,"string"变成了NaN。第二个if语句是有意义的,但我不明白为什
function ok(value, message) {
if (!!!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
!!!value的意思是not not not Boolean(value),是吗?
如果是value = 9 > 1,那就意味着:not not not true。
not true = false
not not true = true
not not not true = false
我的大脑很疼。他们为什么不直接用!value而不是!!!value
我有一小部分代码:
$company = array('relation' => $_SESSION['username']);
$companyresponse = $wcfclient->GetCompany($company);
foreach ($companyresponse->GetCompanyResult as $key => $value){
echo $value[0]; //This is the name of the company
if ($value[1] == TRUE){
为什么这两种代码有不同的答案:
代码1答案为假代码2答案为真
代码1
var x = Boolean (false);
if (x)
{
console.log (true);
}
else
{
console.log (false);
}
// answer is false
code2
var x = new Boolean (false);
if (x)
{
console.log (true);
}
else
{
console.log (false);
}
//answer is true