javascript中的布尔条件

Boolean conditions in javascript

本文关键字:条件 布尔 javascript      更新时间:2023-09-26

无法理解如何检查函数中给定的值。我所有已知的方法都失败了。抱歉问了个蹩脚的问题!:)

# doesn't work, but should to
window.mobilecheck = function() {
    return true;
}
if (window.mobilecheck==true) {
    alert('true');
}
# works, but shouldn't
window.mobilecheck = function() {
    return false;
}
if (window.mobilecheck) {
    alert('true');
}
# doesn't work, but should
window.mobilecheck = function() {
    return 1;
}
if (window.mobilecheck==1) {
    alert('true');
}

那么,如果不起作用,如何检查函数是否返回true或false呢?

当您使用window.mobilecheck时,您得到的是函数,而不是函数的求值/返回值。使用window.mobilecheck() ==1来评估您的功能。