最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

underscore.js - The order of operators in JavaScript (|| ,&&) - Stack Overflow

programmeradmin5浏览0评论

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 operate 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:

  1. Why !!obj should exist? if we delete !!obj, code run as well.

  2. the operator order of this code? I know && operator are higher than ||, so I guess !!obj effect when obj is null, but when I practice that is no what I want;

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 operate 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:

  1. Why !!obj should exist? if we delete !!obj, code run as well.

  2. the operator order of this code? I know && operator are higher than ||, so I guess !!obj effect when obj is null, but when I practice that is no what I want;

Share Improve this question edited Dec 2, 2016 at 15:03 Emile Bergeron 17.4k5 gold badges85 silver badges131 bronze badges asked Dec 2, 2016 at 8:56 L.AnnL.Ann 881 silver badge9 bronze badges 3
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – mplungjan Commented Dec 2, 2016 at 8:57
  • 1 Question is, since null is an object in Javascript, why would they want their _.isObject method to return false on null? – connexo Commented Dec 2, 2016 at 9:11
  • 2 @connexo well, because in the vast majority of cases, you want to check if what you get is an object with a value, rather than an object representing the absence of a value. Besides, were to actually make that argument for real (I am not sure if you're just playing devil's advocate here), then I'd point out that null instanceof Object// false while both {} and function(){} will return true for that. Thus, the isObject function is entirely consistent with those. Which, again, happens to be the most mon use-case for such a check. – VLAZ Commented Dec 2, 2016 at 11:48
Add a ment  | 

4 Answers 4

Reset to default 5

They want to return false if the object is null. Usually when we need to know if something is an object, null is not what we're looking for. That's because trying to access null's properties (null[propName] for example) would throw an error.

console.log(typeof null);

The order of execution for the expression type === 'function' || type === 'object' && !!obj; is from left to right:

  1. type === 'function' - if this is true the expression will return true without puting the rest
  2. type === 'object' - if this is false the expression will return false without puting the last part
  3. !!obj - null would return false, any other object would return true

The snippet demonstrates the flow:

step(false, 1) || step(true, 2) && step(true, 3)

function step(ret, step) {
  console.log(step);
  
  return ret;
}

Using !! we can cast values to booleans - So truthy values would be converted to true, for example !!{} === true, and falsy ones to false, for example !!null === false.

Firstly, the expression is not read left to right, && has a slightly higher precedence than ||, so it can be written either way -- a && b || c is identical to c || a && b. Operator precedence determines where parenthises should go in the expression, so a && b || c would be (a && b) || c. It's only within expressions where every operator has the same precedence that the operators are evaluated from left to right.

Regarding you're actual question -- in javascript, typeof null === "object", so the && !!obj part of the expression is to guard against null values evaluating to true.

The last

!!obj

forces the return value to a boolean value and yes, it is necessary because of typeof null is object.

Just Reference to MDN's Operator Precedence.

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

Precendence for && is higher than ||

&& first

发布评论

评论列表(0)

  1. 暂无评论