我正在阅读《面向Web开发人员的专业Javascript》一书,当作者说:
“错误地使用对象而不是对象
bool(boolean) 值可以极大地改变您的应用程序流程。”
谁能用一个例子再解释一下...谢谢
最佳答案
这主要是警告您不要对容易执行“偶然”返回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"