zoukankan      html  css  js  c++  java
  • 重写数组拓展方法

    forEach
    Array.prototype.myforEach function (fn) {
        var arr = this,
            length = arr.length,
            newThis = arguments[1] || global;
        for (var index = 0; index < length; index++) {
            fn.apply(newThis, [arr[index], index, arr])
        }
    
    }
    map Array.prototype.myMap 
    function (fn) {     var arr = this,         length = arr.length,         newThis = arguments[1] || global;     newArr = [];     for (var index = 0; index < length; index++) {         newArr.push(fn.apply(newThis, [arr[index], index, arr]))     }     return newArr }
    filter Array.prototype.myFilter 
    function (fn) {     var arr = this,         length = arr.length,         newThis = arguments[1] || global,         newArr = [];     for (var index = 0; index < length; index++) {         fn.apply(newThis, [arr[index], index, arr]) ? newArr.push(arr[index]) : null     }     return newArr }
    every Array.prototype.myEvery 
    function (fn) {     var arr = this,         length = arr.length,         newThis = arguments[1] || global;     for (var index = 0; index < length; index++) {         if (!fn.apply(newThis, [arr[index], index, arr])) {             return false         }     }     return true }
    some Array.prototype.mySome 
    function (fn) {     var arr = this,         length = arr.length,         newThis = arguments[1] || global;     for (var index = 0; index < length; index++) {         if (fn.apply(newThis, [arr[index], index, arr])) {             return true         }     }     return false }
    reduce Array.prototype.myReduce 
    function (fn, initValue) {     var arr = this,         length = arr.length,         newThis = arguments[2] || global;     for (var index = 0; index < length; index++) {         initValue = fn.apply(newThis, [initValue, arr[index], index, arr])     }     return initValue }
    reduceRight Array.prototype.myReduceRight 
    function (fn, initValue) {     var arr = this,         length = arr.length,         newThis = arguments[2] || global;     for (var index = length; index--;) {         initValue = fn.apply(newThis, [initValue, arr[index], index, arr])     }     return initValue }
  • 相关阅读:
    POJ 1017
    poj 2709
    poj 1328
    POJ 2386
    POJ 1065
    POJ 3728
    hdu--1004--Let the Balloon Rise
    hdu--2570--迷瘴(贪心)
    hdu--1257--最少拦截系统(贪心)
    hdu--1230--火星A+B
  • 原文地址:https://www.cnblogs.com/ssszjh/p/14348341.html
Copyright © 2011-2022 走看看