javascript - 检查 bool 正在检查对象

标签 javascript

我在 JS 中创建了这个实用方法:

function IsAuthenticated(userID)
{
    var isAuthed = false;

    if (userID.length == 0)
        return false;

    // more logic
    if(SomeLogic)
       isAuthed = true;

    return isAuthed;
}

当我运行这样的东西时,我得到一个对象而不是类型 bool:

if(IsAuthenticated)
    //code here

我认为我需要将其转换为 bool 值?

最佳答案

IsAuthenticated 指的是名为“IsAuthenticated”的函数,而不是函数调用。如果您在 IsAuthenticated 上使用 typeof 运算符,您将得到 "function":

alert(typeof IsAuthenticated);

所以试试这个:

var userID = /* … */;
if (IsAuthenticated(userID)) {
    //code here
}

关于javascript - 检查 bool 正在检查对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284926/

相关文章:

javascript - 如何使用 Watir 在网页中添加并执行 JavaScript 函数

javascript - 尝试在获取长度值时在 JavaScript 中设置字体大小

javascript - 使用 jquery 中的表单将值插入到表中

javascript - 如何使用 jQuery 从 AJAX 请求获取响应文本

javascript - Discord.JS - 使用角色将 Discord 中的用户移动到您的 VC

javascript - 用视频 ID 替换 youtube 嵌入 iframe - Javascript

javascript - jQuery 选择器 : match array attribute's n-th value

javascript - 如何将firebase中的图像url显示到html表中?

javascript - AngularJS Factory 变量值分配打破绑定(bind)

javascript - 为什么我的迭代器变量不断重置?