underscore.js库中的每个实现

use*_*110 6 javascript

关于我在underscore.js源代码中找到的"each"函数的实现的问题(下面的源代码).

首先,有人可以解释"else if(obj.length === + obj.length)"正在检查的行.

第二,有人可以解释为什么使用hasOwnProperty.call(obj,key),而不是obj.hasOwnProperty?是因为传入的obj可能没有实现hasOwnProperty(我认为每个javascript对象都有)

任何见解都表示赞赏.谢谢.

  // The cornerstone, an `each` implementation, aka `forEach`.
  // Handles objects with the built-in `forEach`, arrays, and raw objects.
  // Delegates to **ECMAScript 5**'s native `forEach` if available.

  var each = _.each = _.forEach = function(obj, iterator, context) {

    if (obj == null) return;
    if (nativeForEach && obj.forEach === nativeForEach) {
      obj.forEach(iterator, context);
    } else if (obj.length === +obj.length) {
      for (var i = 0, l = obj.length; i < l; i++) {
        if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;

      }
    } else {
      for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) {
          if (iterator.call(context, obj[key], key, obj) === breaker) return;
        }
      }
    }
  };
Run Code Online (Sandbox Code Playgroud)

use*_*716 5

这个:

+obj.length
Run Code Online (Sandbox Code Playgroud)

...将对值进行toNumber转换length.

看起来他们确保length通过执行toNumber转换来引用数字,并验证转换后它仍然是相同的数字.

如果是这样,他们认为它是一个数组,或者至少是一个类似于数组的对象用于迭代.

如果不是,则他们假定需要枚举所有键值对.

var obj = {
     length:null,
     someprop:'some value'
};

obj.length === +obj.length; // false, so do the enumeration
Run Code Online (Sandbox Code Playgroud)
var obj = {
    length: 2,
    "0":'some value',
    "1":'some other value'
};

obj.length === +obj.length;  // true, not an actual Array, 
                             //     but iteration is still probably wanted
Run Code Online (Sandbox Code Playgroud)

当然,您可以拥有一个具有length原始数字属性的对象,但仍打算枚举属性.

var obj = {
    length: 2,
    "prop1":'some value',
    "prop2":'some other value'
};

obj.length === +obj.length;  // true, it will iterate, but it would
                             //           seem that enumeration is intended
Run Code Online (Sandbox Code Playgroud)