tags:

views:

66

answers:

3

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?

A: 

I think you need:

return isAuthed;
scunliffe
+1  A: 

Try return isAuthed instead of just isAuthed.

Jacob
+6  A: 

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
}
Gumbo
ok so you're never able to first set a returned value from a method to a var first? I don't want to put in the full function into an if statement, it's hacky. (nothing against you)
CoffeeAddict
in fact I had already tried that anyway
CoffeeAddict
It doesn't matter either way. I should be able to set that function call to a var. Either way I get false every time no matter what. I know it's entering the SomeLogic for the setting of true but it's returning false ultimately for some reason still even though it's hitting the line to set it to true in the IsAuthenticated method
CoffeeAddict
It's working fine...thought it was not but it was.
CoffeeAddict