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

posted @ 2020-08-23 22:12  荣光无限  阅读(139)  评论(0编辑  收藏  举报