call、aply、bind的常用方法总结

  • 类函数变为数组
    function aaa (){
        Array.prototype.slice(arguments);
    }

进一步操作它的每个元素

function bbb() {
    Array.prototype.slice(arguments).forEach(function(element, index) {
      // 可以操作每一个的element
    })
}
  • 数组之间的追加
    var arr1 = [12 , "foo" , {name "Joe"} , -2458];
    var arr2 = ["Doe" , 555 , 100];
    Array.prototype.push.apply(arr1, arr2);
  • 数组之间求最大值最小值
    var numbers = [5, 458 , 120 , -215];
    var maxNumber = Math.max.apply(Math, numbers);
    var maxNumber = Math.max.call(Math, 5, 458, 120, -215);
  • 验证是否是数组
    function isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
    }
  • 优雅的数组降级
    function reduceDimension(arr) {
        var reduced = [];
        for(var i = 0; i < arr.length; i++){
            reduced = reduced.concat(arr[i]);
        }

        return reduced;
    }
    function reduceDimension(arr){
        return Array.prototype.concat.apply([], arr);
    }
原文地址:https://www.cnblogs.com/mayufo/p/5850148.html