概述
柯里化其实就是高阶函数,是其应用之一。
当对功能定制功能(函数)时,可预先配置部分参数,在使用时接受剩余函数。
当然,也可以不使用柯里化,但会在多处存在同样的调用模式,通常参数是固定的(也可动态化配置),不利用维护。
示例
function renderText(length) { return function (value) { value = value || '' const shoudSlice = value.length > length const sliceValue = shoudSlice ? value.slice(0, length) + '...' : value return shoudSlice ? sliceValue : value } } export const renderUrl = renderText(30) export const renderDefault = renderText(10) export const renderShort = renderText(6)
参数length属于差异化配置,通过柯里化生成差异化功能函数,方便后续直接使用。