gpt4 book ai didi

javascript - 使用对象而不是 bool 更改应用程序流

转载 作者:行者123 更新时间:2023-12-01 15:38:14 26 4
gpt4 key购买 nike

我正在阅读“面向 Web 开发人员的专业 Javascript”一书,当作者说:
“错误地使用对象而不是
bool 值可以极大地改变应用程序的流程。”
enter image description here
任何人都可以用一个例子来解释一下......谢谢

最佳答案

它主要是警告您不要对很容易“意外”返回 true 的对象执行等价运算符:

let myTestBoolean = false;
let myTestObject = { result: false };

if (myTestBoolean) {
console.log("boolean is true");
} else {
console.log("boolean is false");
}
//results in "boolean is false"

if (myTestObject) {
console.log("object is true");
} else {
console.log("object is false");
}
//results in "object is true"

关于javascript - 使用对象而不是 bool 更改应用程序流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63371167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com