Es5常用方法兼容实现与一些新方法

Function:bind
Array:some,map,every,filter,reduce,reduceRight,indexOf,lastIndexOf ,isArray(Array.isArray([]))
String: trim ,'sdfd'[2] // 'f'
Date: Date.now() // 1490106171645

bind

if (!function() {}.bind) {
   
    Function.prototype.bind = function(){ 
        var self = this, // 保存原函数
        context = [].shift.call( arguments ),
        args = [].slice.call( arguments );
        return function(){ 
            // 返回一个新的函数
            // 需要绑定的 this 上下文 // 剩余的参数转成数组
            return self.apply( context, [].concat.call( args, [].slice.call( arguments ) ) ); 
            // 执行新的函数的时候,会把之前传入的 context 当作新函数体内的 this
            // 并且组合两次分别传入的参数,作为新函数的参数
        } 
    };
}

[1,2,4].map(function(){})

if(!Array.prototype.map){
     Array.prototype.map=function (fn,context) {
         var context = context;
         var result = [];
         if (typeof fn === "function") {
             for(var i=0,len=this.length;i

[1,2,3,4].foreach(function(){})

if(!Array.prototype.myForEach){
        Array.prototype.myForEach = function myForEach(callback,context){
            var context = context;
            if(typeof callback === 'function'){
                for(var i = 0,len = this.length; i < len;i++) {
                    callback && callback.call(context,this[i],i,this);
                }
            }
        }
    }

[1,2,3,4].filter(function(){})

    if (typeof Array.prototype.filter != "function") {
        Array.prototype.filter = function (fn, context) {
            var arr = [];
            if (typeof fn === "function") {
                for (var i = 0, length = this.length;  i< length; i++) {
                    if(fn.call(context, this[i], i, this)){
                        arr.push(this[i]);
                    }
                }
            }
            return arr;
        };
    }

some 数组的方法,只要一个满足,就返回true

if (typeof Array.prototype.some != "function") {
  Array.prototype.some = function (fn, context) {
    var passed = false;
    if (typeof fn === "function") {
      for (var k = 0, length = this.length; k < length; k++) {
          if (passed === true) break;
          passed = !!fn.call(context, this[k], k, this);
      }
    }
    return passed;
  };
}

every 数组的方法 和some相对应

if (typeof Array.prototype.every != "function") {
  Array.prototype.every = function (fn, context) {
    var passed = true;
    if (typeof fn === "function") {
       for (var k = 0, length = this.length; k < length; k++) {
          if (passed === false) break;
          passed = !!fn.call(context, this[k], k, this);
      }
    }
    return passed;
  };
}

indexOf 数组的方法

var data = [2, 5, 7, 3, 5];
console.log(data.indexOf(5, "3")); // 4 (从3号位开始搜索)
if (typeof Array.prototype.indexOf != "function") {
  Array.prototype.indexOf = function (searchElement, fromIndex) {
    var index = -1;
    fromIndex = fromIndex * 1 || 0;
    for (var k = 0, length = this.length; k < length; k++) {
      if (k >= fromIndex && this[k] === searchElement) {
          index = k;
          break;
      }
    }
    return index;
  };
}

lastIndexOf 数组的方法

if (typeof Array.prototype.lastIndexOf != "function") {
  Array.prototype.lastIndexOf = function (searchElement, fromIndex) {
    var index = -1, length = this.length;
    fromIndex = fromIndex * 1 || length - 1;

    for (var k = length - 1; k > -1; k-=1) {
        if (k <= fromIndex && this[k] === searchElement) {
            index = k;
            break;
        }
    }
    return index;
  };
}

reduce 好厉害,以前竟然从来没用过

var sum = [1, 2, 3, 4].reduce(function (previous, current, index, array) {
  return previous + current;
});
console.log(sum); // 10

var matrix = [
  [1, 2],
  [3, 4],
  [5, 6]
];
// 二维数组扁平化
var flatten = matrix.reduce(function (previous, current) {
  return previous.concat(current);
});
console.log(flatten); // [1, 2, 3, 4, 5, 6]
// 兼容处理
if (typeof Array.prototype.reduce != "function") {
  Array.prototype.reduce = function (callback, initialValue ) {
     var previous = initialValue, k = 0, length = this.length;
     if (typeof initialValue === "undefined") {
        previous = this[0];
        k = 1;
     }
    if (typeof callback === "function") {
      for (k; k < length; k++) {
          // 没有对上下文的切换,所以不需要call,apply了
         this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
      }
    }
    return previous;
  };
}

reduceRight array的方法

var data = [1, 2, 3, 4];
var specialDiff = data.reduceRight(function (previous, current, index) {
  if (index == 0) {
    return previous + current;
  }
  return previous - current;
});
console.log(specialDiff); // 0
//兼容处理
if (typeof Array.prototype.reduceRight != "function") {
  Array.prototype.reduceRight = function (callback, initialValue ) {
    var length = this.length, k = length - 1, previous = initialValue;
    if (typeof initialValue === "undefined") {
        previous = this[length - 1];
        k--;
    }
    if (typeof callback === "function") {
       for (k; k > -1; k-=1) {          
          this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
       }
    }
    return previous;
  };
}
Object
所有对象操作中,如果o不是Object类型,将会抛出TypeError异常。
Object.getPrototypeOf(o)

获取给丁对象的prototype对象。等价于以前的o.__proto__。
Object.getOwnPropertyDescriptor(o,p)

获取对象描述。和Object.defineProperty的相关方法。
Object.getOwnPropertyNames(o)

获取自有属性名列表。结果列表将不包含原型链上的属性。
Object.create(o,p)

以给丁对象o为prototype创建新的对象并返回。如果对象描述p存在,就使用其定义刚创建的对象(类似调用Object.defineProperties(obj,p))。
Object.defineProperty(o,p,attrs)

根据规则attrs定义对象o上,属性名为p的属性Object.defineProperties(o,props)

根据对象描述props来定义对象o,通常props包含多个属性的定义。
Object.seal(o)

一个对象在默认状态下,extensible: 可以添加新的属性configurable: 可以修改已有属性的特性

Object.seal会改变这两个特性,既不能扩展新属性,也不能修改已有属性的特性。Object.freeze(o)

将对象的每个自有自有属性(own property)做如下操作:
属性的writable,特性置为false,属性的configurable,特性置为false

同时,该对象将不可扩展。可见,该方法比Object.seal更加严格的限制了对一个对象的未来改动。Object.preventExtensions(o)
将对象置为不可扩展。Object.isSealed(o)
判断一个对象是否sealed:
对象的每个自有属性:如果属性的configurable特性为true,则返回false
如果对象为extensible的,那么返回false
不满足以上两个条件,则返回true

Object.isFrozen(o)对每个自有属性,如果该属性的configurable或writable
特性为true,则返回false,如果对象为extensible的,那么返回false
不满足以上两个条件,则返回true

Object.isExtensible(o)
判对一个对象是否可扩展。

Object.keys(o)
返回对象o的所有可枚举(enumerable)属性的名称。

Object.prototype.isPrototypeOf(v)
检查对象是否是位于给定对象v的原型链上。
Object.prototype.propertyIsEnumerable(p)
检查一个对象上的属性p是否可枚举。

参照 张鑫旭[http://www.zhangxinxu.com/wordpress/?p=3220]
与https://segmentfault.com/a/1190000000515151

你可能感兴趣的:(Es5常用方法兼容实现与一些新方法)