I read the source code of Underscore.js, then something confused me:
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
I am confused by the operator order of expression.
I think operator precedence in
return type === 'function' || type === 'object' && !!obj;
will be from leftto right; I mean:
return (type === 'function' ) || ( type === 'object' && !!obj);
if typeequal to functionreturn true; work differently type === 'object' && !!obj; if typeequal to objectreturn !!obj, same as Boolean(obj); else return false;
I made some examples:
var a = alert(1) || alert(2) && alert(3);
alert(a);
var a = alert(1) || alert(2) && 0;
alert(a);
what confused me:
Why !!objshould exist? if we delete !!obj, also run the code.
operator order of this code? I know that the statement is &&higher than ||, so I think the !!objeffect is when obj is null, but when I train, this is not what I want;