underscore源码分析之JS API

3 篇文章 0 订阅

github:https://github.com/jingegebuguai/underscore-context

数组对象遍历(each)

each方法主要实现js的foreach函数:

目标:

    var arr = [1, 2, 3]
    arr.each((ele, index) => {
        console.log(ele, index) //1,0 2,1 3,2
    })

实现代码:

    var _each = function(fn) {
        if(typeof fn === 'function') {
            var i, length = this.length
            if(length === +length) {
                for(i = 0; i < length; i++) {
                    fn.call(null, this[i], i, this)
                }
            } else {
                var keys = Object.keys(this)
                for(i = 0, length = keys.length; i < length; i++) {
                    fn.call(null, this[keys[i]], keys[i], this)
                }
            }
        }
        return this
    }

    Array.prototype.each = _each
    Object.prototype.each = _each

    var arr = {name: 'xxx', age: 18}
    arr.each((ele, index) => {
        console.log(ele, index)
    })

Map API

实现对数组和对象的map操作

目标:

    var arr = [1, 2, 3]
    var newArr = arr.map((ele, index) =>{
        return ele*2
    })
    console.log(newArr) //[2, 4, 6]

实现代码:

    var newMap = function(fn) {
        var keys = this.length !== +this.length && Object.keys(this)
        var length = (keys || this).length, currentKey
        var result = keys? {} : Array(length)
        for(var i = 0; i < length; i++) {
            currentKey = keys? keys[i] : i
            result[currentKey] = fn.call(null, this[currentKey], currentKey, this)
        }
        return result
    }

    Array.prototype._map = newMap
    Object.prototype._map = newMap


    var arr1 = {name: 'xxx', age: '18'}
    var arr2 = [1, 2, 3]
    var obj = arr1._map((ele, index) => {
        return ele*2
    })
    console.log(obj) //{ name: NaN, age: 36 }

Reduce API

实现对数组或对象的reduce操作

目标:

    var arr = [1, 2, 3]
    var sum = arr.reduce((prev, next) => {
        return prev + next
    })
    console.log(newArr) //6

实现代码

    var newReduce = function(fn, initValue) {
        var keys = this.length !== +this.length && Object.keys(this)
        var length = (keys || this).length, currentKey
        var result, i = 0, key
        if([].slice.call(arguments, 1) == undefined || arguments.length < 2) {
            result = keys? this[keys[i++]] : this[i++]
        } else {
            result = initValue
        }
        for(; i < length; i++) {
            key = keys? this[i] : i
            result = fn.call(null, result, this[key], key, this)
        }
        return result
    }

    Array.prototype._reduce = newReduce
    Object.prototype._reduce = newReduce


    var arr1 = {name: 'xxx', age: '18'}
    var arr2 = [1, 2, 3]
    var sum = arr2._reduce((prev, next) => {
        return prev + next
    })
    console.log(sum)

Filter API

实现对数组或对象的过滤操作

目标

    var arr = [1, 2, 3, 4, 5, 6]
    var result = arr.filter(ele => {
        return ele % 2 == 0
    })
    console.log(result)

实现代码

    var newFilter = function(fn) {
        var length = this.length
        var result = Object.prototype.toString.call(this) === '[object, Array]'? [] : {}
        if(length === +length) {
            for(var i = 0; i < length; i++) {
                if(fn.call(null, this[i], i, this)) {
                    result.push(this[i])
                }
            }
        } else {
            var keys = Object.keys(this)
            for(var i = 0; i < keys.length; i++) {
                if(fn.call(null, this[keys[i]], keys[i], this)) {
                    result[keys[i]] = this[keys[i]]
                }
            }
        }
        return result
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值