开发者

Check for bool is checking for an object instead

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜