views:

183

answers:

3

From MS AJAX source code,

Type.isClass = function Type$isClass(type) {
/// <summary locid="M:J#Type.isClass" />
/// <param name="type" mayBeNull="true"></param>
/// <returns type="Boolean"></returns>
var e = Function._validateParams(arguments, [
    {name: "type", mayBeNull: true}
]);
if (e) throw e;
if ((typeof(type) === 'undefined') || (type === null)) return false;
return !!type.__class;

}

Please look at the last line of code, why use !! instead of just return type.__class?

Thanks!

A: 

Guaranteed safe boolean conversion.

annakata
Is it any different than: return Boolean(type.__class) in terms of safety?
Ates Goral
@Ates Goral: The results should be exactly the same - !! is merely more concise.
Shog9
I'm told that !! is more efficient in C, so perhaps it is in JS too, otherwise yes just conciseness
annakata
+8  A: 

The author must not have thought type.__class was guaranteed to be a boolean value. Since you can pass any object to Type.isClass():

Type.isClass(3);
Type.isClass({});
Type.isClass(AnActualClassFunction);

...there's really no guarantee that type.__class will have a boolean value.

Of course, parameters that aren't ASP.NET AJAX "classes" generally won't have a __class property - so if the routine simply returned type.__class, the possible return values would be true, false, and undefined. This is undesirable for a routine intended to return a boolean.

!!type.__class just guarantees that a boolean value (true or false) is returned (undefined will be converted to false).

Triptych
wow - you edited that a lot. uh, thanks. (I understand Javascript pretty well. ASP.NET, not so much)
Triptych
Your answer piqued my curiosity - i'm not a heavy ASP.NET AJAX user either, but figured there must be a likely scenario where __class wouldn't have a boolean value already. Hope you don't mind the additions!
Shog9
So how does this work? I don't get rep points for this because it's been edited?
Triptych
You should - perhaps you've hit your rep limit (200pts) for today?
Shog9
oh wow, I think maybe I have. that's what I get for late-night SOing I guess.
Triptych
Heh, yeah... The reset time is 0:00GMT, which kinda punishes after-work SOing by those of us in the US.
Shog9
Do you know if these rep points will carry over and be rewarded tomorrow? Or are the lost forever? Couldn't figure it out from the faqs
Triptych
They're effectively gone. When you hit the limit, it's a good time to take a break from answering questions and do something else (edit/re-tag other posts, actual work ;-))
Shog9
A: 

So basically there is no difference between !!val and Boolean(val)

var val; // val is undefined
alert(!!val); // display false
alert(Boolean(val)); // display false as well
That is correct. The same rules are used to coerce val to a boolean in both cases. !! is familiar to C++ users, and somewhat more concise, hence its popularity. Note that "new Boolean(val)" produces a completely different result, which may also help account for a reluctance to use Boolean(val).
Shog9