JavaScript 函数式编程的一个例子

定义柯里化函数

/**
 *
 * @param {*} fromYear: 获取的起始年份
 */
export function getYears(fromYear) {
  /**
   * @param {*} toYear: 获取的中止年份
   */
  return function (toYear) {
    const result = []
    for (let i = fromYear; i <= toYear; i++) {
      result.push({ value: i + '', label: i + '年' })
    }
    return result.reverse()
  }
}

调用

import { getYears } from '@/utils/index.js'
const getYearsFrom2019 = getYears(2019) // From which year
const yearsOptions = getYearsFrom2019(2020) // Until which year

Keep learning
原文地址:https://www.cnblogs.com/leslie1943/p/13551056.html