开发者

Check for bool is checking for an object instead

开发者 https://www.devze.com 2023-01-07 19:10 出处:网络
I\'ve created this utility method in JS: function IsAuthenticated(userID) { var isAuthed = false; if (userID.length == 0)

I've created this utility method in JS:

function IsAuthenticated(userID)
{
    var isAuthed = false;

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

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

    return 开发者_运维技巧isAuthed;
}

When I run something like this, I'm getting an object back rather than type bool:

if(IsAuthenticated)
    //code here

I assume I need to cast it to a bool?


IsAuthenticated refers to the function with the name “IsAuthenticated” and is not a function call. If you use the typeof operator on IsAuthenticated you will get "function":

alert(typeof IsAuthenticated);

So try this instead:

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


Try return isAuthed instead of just isAuthed.


I think you need:

return isAuthed;
0

精彩评论

暂无评论...
验证码 换一张
取 消