我尝试创建绑定层次结构,以便我可以轻松地构建一些预定义的操作。似乎Action函数出了问题,因为它在绑定时并没有真正起作用。
const Action = (channel: string) => {
return (action: string) => {
return (payload: object) => {
return Object.assign({},payload,{channel: channel, action: action});
};
};
};
const WeatherAction = Action.bind('weather');
const WeatherActionSub = WeatherAction.bind('sub');
const WeatherActionUnSub = WeatherAction.bind('unsub');
const test = WeatherActionSub({test: 'test'});
console.log(test);
Bind的第一个参数改变了this
关键字。你只需要在这里打电话,因为这个功能无论如何:
const WeatherAction = Action('weather');
const WeatherActionSub = WeatherAction('sub');
const WeatherActionUnSub = WeatherAction('unsub');
const test = WeatherActionSub({test: 'test'});
console.log(test);
(有效的绑定代码,这是毫无意义的,因为没有参数的调用必须遵循:)
const WeatherAction = Action.bind(null, 'weather')();
const WeatherActionSub = WeatherAction.bind(null, 'sub')();
const WeatherActionUnSub = WeatherAction.bind(null, 'unsub')();
如果函数未被破坏,那么绑定是有意义的:
const ActionUncurried = (channel: string, action: string, payload: object) => {
return Object.assign({},payload,{channel: channel, action: action});
};
const WeatherAction = ActionUncurried.bind(null, 'weather');
const WeatherActionSub = WeatherAction.bind(null, 'sub');
const WeatherActionUnSub = WeatherAction.bind(null, 'unsub');
const test = WeatherActionSub({test: 'test'});
console.log(test);