• JavaScript——实现一些常用函数


    前言

    如题,实现一些常用的函数,一个小练习;

    内容

    forEach

    /**
     * foreach
     * @param {*} array 
     * @param {*} fn 
     */
    function forEach(array, fn) {
        for (let index = 0; index < array.length; index++) {
           fn(array[index]) 
        }
    }
    
    // test
    let array = [1,2,3,4,5]
    
    forEach(array, function name(params) {
        console.log(params);
    })
    
    

    fliter

    /**
     * fliter
     * @param {*} array 
     * @param {*} fn 
     * @returns 
     */
    function fliter (array, fn) {
        let res = []
        for (let index = 0; index < array.length; index++) {
           if (fn(array[index])) {
               res.push(array[index])
           }
        }
        return res
    }
    
    // test
    let arr = [1,2,3,4,5]
    let r = fliter(arr, function name(params) {
        return params % 2 === 0
    })
    console.log(r);
    

    makeFun

    /**
     * makeFun
     * @param {*} params 
     * @returns 
     */
    function makeFn(params) {
        let msg = "hello fn"
        return function name(params) {
            console.log(msg);
        }
    }
    
    // test
    const fn = makeFn()
    fn()
    
    makeFn()()
    

    once

    /**
     * once
     * @param {*} fn 
     * @returns 
     */
    function once(fn) {
        let done = false
        return function name(params) {
            if (!done) {
                done = true
               return fn.apply(this, arguments)
            }
        }
    }
    
    // test
    let pay = once(function (money) {
        console.log(`支付: ${money}`);
    })
    
    pay(5)
    pay(4)
    pay(3)
    

    map

    /**
     * map
     * @param {*} array 
     * @param {*} fn 
     * @returns 
     */
    const map = (array, fn) => {
        let res = []
        for (const value of array) {
            res.push(fn(value))
        }
        return res
    }
    
    // test
    let arr = [1,2,3,4]
    arr = map(arr, v => v * v )
    console.log(arr);
    

    onece

    /**
     * every
     * @param {*} array 
     * @param {*} fn 
     * @returns 
     */
    const every = (array, fn) => {
        let res = []
        for (const value of array) {
            res = fn(value)
            if (!res) {
                break
            }
        }
        return res
    }
    
    // test
    let arr = [11,12,16, 1]
    let r = every(arr, v => v > 10)
    console.log(r);
    

    some

    /**
     * some
     * @param {*} array 
     * @param {*} fn 
     * @returns 
     */
    const some = (array, fn) => {
        let res = false
        for (const value of array) {
            res = fn(value)
            if (res) {
                break
            }
        }
        return res
    }
    
    let arr = [1, 2, 3, 4]
    let r = some(arr, v => v % 2 === 0)
    console.log(r);
    
    学无止境,谦卑而行.
  • 相关阅读:
    ArrayList与LinkedList区别
    ArrayList底层原理
    nginx启用https访问
    云服务器搭建 Nginx 静态网站
    在云服务器上(CentOS)上安装Node
    文本超出显示省略号CSS
    vue使用改变element-ui主题色
    vue中的select框的值动态绑定
    vue项目对axios的全局配置
    使用crypto-js对数据进行AES加密、解密
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/15257278.html
Copyright © 2020-2023  润新知