b1 背后的逻辑是什么? b1 : b2 在我的 if 条件编码中?

What the logic behind b1 ? b1 : b2 in my coding in if-conditions?

1.Here是作业。

声明一个类似于||但不使用||的函数或操作员。 /**

  1. @param {任何} ??? - 第一个操作数
  2. @param {任何} ??? - 第二个操作数
  3. @returns {any} 与应用 || 相同的结果给定操作数的运算符,顺序

2.Here 是什么尝试(实际上人们给了我关于 return b1 ? b1 : b2 的建议。但我无法理解,并且网上还没找到合适的解释。

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

3.Here是编码测试,上面的代码全部通过了。但是有人能告诉我 b1 的逻辑吗? b1 : b2。我是初学者,请帮助我!

//测试 1

actual = or("bananas", false);

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//测试 2

actual = or("", "bananas");

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//测试 3

actual = or(true, true);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//测试 4

actual = or(true, false);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

b1 ? b1 : b2

这意味着如果 b1 是 true/valid 它将简单地 returns 由于 b1 代码的结果,否则它将简单地 return 由于 b2[= 代码的结果11=]

b1 ? b1 : b2 正在使用 Ternary Operation

如果你把它写成 if-else 语句,它看起来像

if (b1 === true) {
  return b1;
} else {
  return b2;
}
function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

很多事情都很开心Headers。在这里,您有一个带有 falsy 检查的测试函数。 Falsy 是 validateNumber,它可以是 null、空白字符串或零。条件将始终 return 为假。如果你真的想用你应该使用 === 的类型来测试值。

console.log(Boolean("")) //false
console.log(Boolean(null))//false

console.log(Boolean(0))//false

console.log(Boolean(" "))//true
console.log(Boolean("1"))//true

您可以像这样替换 or 函数。

function or(b1, b2) {
  if (b1) return b1;
  if (b2) return b2;
  return false;
}

b1 ? b1: b2 正在使用三元运算

简单来说可以理解为

   condition ? (statement when condition is True) : (statement when condition is false)

由于使用的是three operands,故称为三元运算

所以,b1 ? b1: b2 表示如果 b1 为真,则 return b1 else return b2.

您也可以将其拆分为 if else 语句,如下所示:

if(b1 === true)
 return b1;
else 
 return b2;

帮助您理解条件(三元)运算符在JavaScript中的示例,等等b1 ? b1 : b2

function whatUserTyped(str) {
    let b1 = str;
    let b2 = '-Nothing-';

    if (b1) {
        return b1;
    } else {
        return b2;
    }
}

function whatUserTypedButConditionalOperator(str) {
    let b1 = str;
    let b2 = '-Nothing-';

    let rst = b1 ? b1 : b2;

    return rst;
}

let str1 = 'hahaha'; //User typed 'hahaha' into a input[type=text] or textarea or whatever;
let str2 = ''; //Empty string value. User left the input or textarea empty.

console.log('You said ' + whatUserTyped(str1)); //'You said hahaha'
console.log('You said ' + whatUserTyped(str2)); //'You said -Nothing-'

console.log('You said ' + whatUserTypedButConditionalOperator(str1)); //'You said hahaha'
console.log('You said ' + whatUserTypedButConditionalOperator(str2)); //'You said -Nothing-'

条件(三元)运算符条件? exprIfTrue : exprIfFalse。它是 if...else 语句的一种语法糖形式。

好吧 b1 ? b1 : b2 非常棘手。大多数情况下,如果 b1 是假的,不建议 return 另一个 str/obj 或任何立即。

大部分时间是这样使用的:a ? b : c

function isBob(thatGuy) {
    let a;

    if (thatGuy === 'Bob') {
        a = true;
    } else {
        a = false;
    }

    b = 'That Guy is Bob';
    c = 'That Guy is not Bob';

    return (a ?  b : c);
}

console.log(isBob('Bob')); //That Guy is Bob
console.log(isBob('Jimmy')); //That Guy is not Bob

a 被认为是布尔值,大多数时候 aTruthy,因为在 JavaScript、All 中值是真实的,除非它们被定义为虚假(即除了 false、0、-0、0n、""、null、undefined 和 NaN),因此棘手的 b1 ? b1 : b2 不是很合适。

参考文献: