<html>
  <head>

  </head>
  <body>
    <script type="text/javascript">
      Array.prototype.myForEach = function(fn) {
        var arr = this,
          len = arr.length,
          arg2 = arguments[1] || window;
        for(var i = 0; i < len; i++) {
          fn.apply(arg2, [arr[i], i, arr]);
        }
      };

      Array.prototype.myFilter = function(fn) {
        var arr = this,
          len = arr.length,
          arg2 = arguments[1] || window,
          newArr = [];
        for(var i =0; i < len; i++) {
          var item = deepClone(arr[i]);
          fn.apply(arg2, [item, i, arr])? newArr.push(item): ''
        }

        return newArr;
      }

      Array.prototype.myMap = function(fn) {
        var arr = this,
          len = arr.length,
          arg2 = arguments[1] || window,
          newArr = [];
        for(var i =0; i < len; i++) {
          var item = deepClone(arr[i]);
          newArr.push(fn.apply(arg2, [item, i, arr]));
        }
        return newArr;
      }

      Array.prototype.myReduce = function(fn, initialValue) {
       var arr = this,
        len = arr.length,
        arg2 = arguments[2] || window;
      for(var i = 0; i < len; i++) {
        initialValue = fn.apply(arg2, [initialValue, arr[i], i, arr]);
      }
      return initialValue;
     }

     Array.prototype.myEvery = function(fn) {
       var arr = this,
        len = arr.length,
        arg2 = arguments[1] || window,
        flag = true;
      for(var i = 0; i < len; i++) {
        if(!fn.apply(arg2, [arr[i], i, arr])) {
          flag = false;
          break;
        }
      }
      return flag;
     }

     Array.prototype.mySome = function(fn) {
       var arr = this,
        len = arr.length,
        arg2 = arguments[1] || window,
        flag = false;
      for(var i = 0; i < len; i++) {
        if(fn.apply(arg2, [arr[i], i, arr])) {
          flag = true;
          break;
        }
      }
      return flag;
     }

      function deepClone(source, target) {
        var target = target || {};
        for(var key in source) {
          if(typeof(source[key]) === 'object' && source[key] !== null) {
            if(Object.prototype.toString.call(source[key]) === '[object Object]') {
              target[key] = {};
            } else if(Object.prototype.toString.call(source[key]) === '[object Array]') {
              target[key] = [];
            }
            deepClone(source[key], target[key]);
          } else {
            target[key] = source[key]
          }
        }
        return target;
      }

      var arr = [{id: 1}, {id: 2}];
      arr.myForEach(function(item, index, myArr){
        item.id = item.id +1;
      });

      var arrFilter = arr.myFilter(function(item, index, myArr) {
        return item.id > 2;
      });

      var arrMap = arr.myFilter(function(item, index, myArr) {
        return item.id = item.id + 2;
      });


    var arr1 = [1, 3, 5, 6];
     console.log(arr1.myReduce(function(prev, elem){
       return prev = prev + elem;
     }, 0));
     console.log(arr1.myEvery(function(item){
       return item > 2;
     }));
     console.log(arr1.mySome(function(item){
       return item > 2;
     }));

      console.log(arr);

      var obj1 = {
        a: 1,
        b: {
          c: {
            d: 4
          }
        },
        e: null,
        f: undefined,
        g: false,
        h: [{id: 1}, 2, {child: {name: 'test'}}]
      };
      var obj2 = {};
      deepClone(obj1, obj2);
      console.log(obj2);
    </script>
  </body>
</html>

 

posted on 2019-12-27 10:49  yyy_鸳鸯  阅读(396)  评论(0编辑  收藏  举报