1. 程式人生 > 其它 >Mybatis配置解析

Mybatis配置解析

手寫Reduce

Array.prototype.myReduce = function (...args) {
    const fn = args[0] // 接收函式
    const arr = this // 接收陣列
    let initValue, index;
    let value;

    if (args.length === 2) { // 有初始值
        initValue = args[1]
        value = initValue
        index = 0
    } else { // 無初始值
        index = 1
        value =
arr[0] } for (let i = index; i < arr.length; i++) { // console.log(i) value = fn(value, arr[i]) } return value } var arr = [1, 2, 3, 4, 5] var a = arr.myReduce(function (pre, cur, index, a) { return pre + cur }) console.log(a)