ES6方法源码实现

214 阅读1分钟

这是我参与更文挑战的第9天,活动详情查看: 更文挑战

注:以下是个人理解、如有不对还望指正!

filter

Array.prototype.filter = function(callback, thisArg) {
    if (this == undefined) {
        throw new TypeError('this is null or not undefined');
    }
    if (typeof callback !== 'function') {
        throw new TypeError(callback + 'is not a function');
    }
    const res = [];
    // 让O成为回调函数的对象传递(强制转换对象)
    const O = Object(this);

    // >>>0 保证len为number,且为正整数
    const len = O.length >>> 0;
    console.log(len)
    for (let i = 0; i < len; i++) {
        // 检查i是否在O的属性(会检查原型链)
        if (i in O) {
            // 回调函数调用传参
            if (callback.call(thisArg, O[i], i, O)) {
                res.push(O[i]);
            }
        }
    }
    return res;
}

//使用
let arr = [
    {name:'张三'},
    {name:'李四'}
];
let arr1 = arr.filter( (item,index,arr) =>{
    if(item.name === '张三'){
        return item
    }
})
    console.log(arr1) 

map

Array.prototype.map = function(callback, thisArg) {
    if (this == undefined) {
        throw new TypeError('this is null or not defined');
    }
    if (typeof callback !== 'function') {
        throw new TypeError(callback + ' is not a function');
    }
    const res = [];
    // 同理
    const O = Object(this);
    const len = O.length >>> 0;
    for (let i = 0; i < len; i++) {
        if (i in O) {
            // 调用回调函数并传入新数组
            res[i] = callback.call(thisArg, O[i], i, this);
        }
    }
    return res;
    }

//使用
let arr = [
    {name:'张三'},
    {name:'李四'}
];
let arr1 = arr.map( (item,index,arr) =>{
    console.log(item,index,arr)
    item.age = 19
    return item
})
console.log(arr1)

forEach

Array.prototype.forEach = function(callback, thisArg) {
    if (this == null) {
        throw new TypeError('this is null or not defined');
    }
    if (typeof callback !== "function") {
        throw new TypeError(callback + ' is not a function');
    }
    const O = Object(this);
    const len = O.length >>> 0;
    let k = 0;
    while (k < len) {
        if (k in O) {
            callback.call(thisArg, O[k], k, O);
        }
        k++;
    }
  }

//使用
let arr = [
    {name:'张三'},
    {name:'李四'}
];
arr.forEach( (item,index,arr) =>{
   console.log(item,index,arr)
})

reduce

Array.prototype.reduce = function(callback, initialValue) {
    if (this == undefined) {
        throw new TypeError('this is null or not defined');
    }
    if (typeof callback !== 'function') {
        throw new TypeError(callbackfn + ' is not a function');
    }
    const O = Object(this);
    const len = this.length >>> 0;
    let accumulator = initialValue;
    let k = 0;
    // 如果第二个参数为undefined的情况下
    // 则数组的第一个有效值作为累加器的初始值
    if ( accumulator === undefined ) {
        while (k < len && !(k in O)) {
            k++;
        }
        // 如果超出数组界限还没有找到累加器的初始值,则TypeError
        if (k >= len) {
            throw new TypeError('Reduce of empty array with no initial value');
        }
        accumulator = O[k++];
    }
    while (k < len) {
        if (k in O) {
            accumulator = callback.call(undefined, accumulator, O[k], k, O);
        }
        k++;
    }
    return accumulator;
}
let arr = [
    {name:'张三'},
    {name:'李四'}
];
arr.reduce( (cu,item,index,arr) =>{
   console.log(cu,item,index,arr)
},[])