Underscore.js学习笔记

1. 缓存原型,考虑访问速度

var

    push             = ArrayProto.push,

    slice            = ArrayProto.slice,

    concat           = ArrayProto.concat,

    toString         = ObjProto.toString,

    hasOwnProperty   = ObjProto.hasOwnProperty;

2. 判断数组是否支持forEach方法,判断对象是否有此原型方法

nativeForEach && obj.forEach === nativeForEach

3. Object.keys方法返回对象自身可枚举的属性,不同于hasOwnProperty方法会继承上级属性

4. 函数参数个数多于65535个时,Math.max.apply(Math, array)会发生范围溢出错误

 https://bugs.webkit.org/show_bug.cgi?id=80797

5. 

var arr = [1,2,3,4],

      arr1= [5,6,7,8];

// arr.push.apply(arr,arr1) == arr.push(5,6,7,8)

 6. 数组初始化时,明确长度后再赋值性能较好,参考 http://jsperf.com/array-init-ivon

var pairs = new Array(length);

  for (var i = 0; i < length; i++) {

    pairs[i] = [keys[i], obj[keys[i]]];

}

 7. 数字0与-0的区别

// Identical objects are equal. `0 === -0`, but they aren't identical.

// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).

if (a === b) return a !== 0 || 1 / a == 1 / b;

// 1/0 ===Infinity, 1/-0 === -Infinity

 

你可能感兴趣的:(underscore)