Factory Function
工厂函数返回配置好的函数或对象闭包,用来隐藏创建细节、固化默认值,避免调用方重复拼装参数。
项目中的实现
- 源码:
src/factoryFunction.ts - 导出:
FormatterOptions(类型)、createFormatter、createThresholdChecker
各函数示例
createFormatter
import { createFormatter } from 'wssf-kage-js';
const shout = createFormatter({ prefix: '>> ', uppercase: true });
shout('go'); // => '>> GO'
createThresholdChecker
import { createThresholdChecker } from 'wssf-kage-js';
const pass = createThresholdChecker(60);
pass(59); // => false
pass(60); // => true
什么时候优先用
- 同一逻辑多组预设(主题、环境、租户)
- 希望用闭包持有轻量配置而非到处传 options 对象