15

I have found some code from Underscore.js

  _.map = _.collect = function(obj, iterator, context) {
    var results = [];
    if (obj == null) return results;
    if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
    each(obj, function(value, index, list) {
      results[results.length] = iterator.call(context, value, index, list);
    });
    if (obj.length === +obj.length) results.length = obj.length;
    return results;
  };

I would like to know what if (obj.length === +obj.length) does?

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
Cybrix
  • 3,248
  • 5
  • 42
  • 61
  • Although I understand very well what `a === +a` does, I'd rather the author be explicit in the intentions and use `typeof a === 'number'`, or `Object.prototype.toString.call(a) === '[object Number]'`. – zzzzBov Jan 31 '12 at 15:18
  • @amnotiam, it should probably use an `isFinite` check as `NaN` and `Infinity` are important edge-cases to avoid. – zzzzBov Jan 31 '12 at 15:57

4 Answers4

12

+length is a method to convert anything to a number.

If it's a number, the value doesn't change, and the comparison returns true.
If it's not a number, the assertion is false.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • When you say *"method"* I'm sure you don't mean function/method, but it could be confusing to someone. ;) –  Jan 31 '12 at 15:16
  • @amnotiam `method` in the meaning of `way`. @Cybrix In case you're wondering for the purpose of `if (l == +l) r.l = o.l;` in the code: If the passed object is an array(-like) object, the `results` list also gets the length of the `obj` object. Relevant code is also visible at the `_.each` -> `Array.prototype.forEach` method (function ;)). – Rob W Jan 31 '12 at 15:23
6

That's the unary + operator. This website has a great article on its uses with the different data types in javascript.

http://xkr.us/articles/javascript/unary-add/

I'll steal the introduction, but it is really worth reading if you are into javascript.

In JavaScript it is possible to use the + operator alone before a single element. This indicates a math operation and tries to convert the element to a number. If the conversion fails, it will evaluate to NaN. This is especially useful when one wants to convert a string to a number quickly, but can also be used on a select set of other types.

The unary + operator, when used on types other than string, will internally attempt to call valueOf() or toString() (in that order) and then attempt to convert the result to a number. Thusly, the unary + operator can successfully convert many of the native JS types with certain restrictions:

mrtsherman
  • 39,342
  • 23
  • 87
  • 111
2

This is test, if obj.length is number.

Doing arithmetic operation on string converts it to integer (and + is unary operation.. which doesn't do anything :-) ), and === operator does type-wise comparsion

a === b <=> (a == b) && (typeof a) == (typeof b)

nothrow
  • 15,882
  • 9
  • 57
  • 104
0

I will suggest you to try this

console.log(typeof +"3") = number

console.log(typeof "3") = string

This makes everything clear.

dinesh_malhotra
  • 1,829
  • 17
  • 10