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

What is unary + used for in Javascript? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 29, 2012 at 0:47 andrew cooke 46.9k10 gold badges100 silver badges146 bronze badges asked Jan 31, 2012 at 15:11 CybrixCybrix 3,3185 gold badges43 silver badges62 bronze badges 2
  • 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 Commented Jan 31, 2012 at 15:18
  • @amnotiam, it should probably use an isFinite check as NaN and Infinity are important edge-cases to avoid. – zzzzBov Commented Jan 31, 2012 at 15:57
Add a comment  | 

4 Answers 4

Reset to default 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.

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:

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)

I will suggest you to try this

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

console.log(typeof "3") = string

This makes everything clear.

发布评论

评论列表(0)

  1. 暂无评论