• 数组方法重写:forEach, map, filter, every, some, reduce


    <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>
  • 相关阅读:
    poj 2886 Who Gets the Most Candies?(线段树)
    Android开发之SoundPool使用具体解释
    Win8.1应用开发之动态磁贴
    开发人员获取苹果技术支持
    Win32 Windows编程 十二
    华为上机练习题--压缩字符串
    PS 滤镜算法原理 ——马赛克
    JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
    mybatis入门基础----动态SQL
    windows上如何搭建Git Server
  • 原文地址:https://www.cnblogs.com/ycherry/p/12106123.html
Copyright © 2020-2023  润新知