I am reading the source code of underscore.js, then something confused me;
// Its code, check the passed-in parameter obj
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
I am confused about the operator order of expression;
I think the operator precedence in
return type === 'function' || type === 'object' && !!obj;
will be from left to right; I mean equal to :
return (type === 'function' ) || ( type === 'object' && !!obj);
if type equal function return true; else oprate type === 'object' && !!obj ;if type equal object return !!obj ,same as Boolean(obj); else return false;
I made some examples:
var a = alert(1) || alert(2) && alert(3);
alert(a); //result : 1, 2 undefined;
var a = alert(1) || alert(2) && 0;
alert(a); //result : 1, 2 undefined;
what confused me:
-
why
!!objshould exsit ?if we delete!!obj,code run as well; -
the operator order of this code? I know
&&oprator are higher than||, so I guess!!objeffect when obj is null; but when I pratice that is no what I want;
Aucun commentaire:
Enregistrer un commentaire