${TaskFilter({ filter: state.filter })}
${TaskList({
tasks: visibleTasks,
onToggleTask: id => this.store.dispatch(ActionCreators.toggleTask(id))
})}
${TaskInput({
onAddTask: text => this.store.dispatch(ActionCreators.addTask(text))
})}
JavaScript函数式编程实战:构建不可变数据流应用 | FP编程指南
`;
}
destroy() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
}
// 交互式函数式编程演示
document.addEventListener(‘DOMContentLoaded’, function() {
const demoSection = document.createElement(‘article’);
demoSection.innerHTML = `
函数式编程交互演示
不可变数据操作演示
`;
document.querySelector(‘main’).appendChild(demoSection);
// 演示函数
window.demoImmutableArray = function() {
const original = [1, 2, 3, 4];
const added = […original, 5];
const removed = original.filter(x => x !== 3);
const updated = original.map(x => x * 2);
const output = `
原始数组: [${original}]
添加元素后: [${added}]
删除元素3后: [${removed}]
所有元素乘以2: [${updated}]
原始数组未改变: [${original}]
`;
document.getElementById(‘demoOutput’).textContent = output;
};
window.demoFunctionComposition = function() {
const toUpper = str => str.toUpperCase();
const exclaim = str => str + ‘!’;
const repeat = str => str.repeat(2);
const process = pipe(toUpper, repeat, exclaim);
const result = process(‘hello’);
const output = `
函数组合演示:
输入: “hello”
处理流程: toUpper -> repeat -> exclaim
结果: “${result}”
`;
document.getElementById(‘demoOutput’).textContent = output;
};
window.demoCurrying = function() {
const add = a => b => c => a + b + c;
const add5 = add(5);
const add5And10 = add5(10);
const result = add5And10(15);
const output = `
柯里化演示:
const add = a => b => c => a + b + c;
const add5 = add(5);
const add5And10 = add5(10);
add5And10(15) = ${result}
`;
document.getElementById(‘demoOutput’).textContent = output;
};
});
// 工具函数定义
function pipe(…fns) {
return x => fns.reduce((v, f) => f(v), x);
}
淘吗网 javascript JavaScript函数式编程实战:构建不可变数据流应用 | FP编程指南 https://www.taomawang.com/web/javascript/1102.html
相关文章
- JavaScript模块化开发实战:从CommonJS到ES6模块完整指南 2025-11-11
- JavaScript异步迭代器与生成器深度实战:构建高性能数据流处理系统 2025-11-08
- JavaScript高级数据可视化:使用Canvas构建实时股票交易仪表盘 2025-11-07
- JavaScript Generator与异步迭代器实战:构建可暂停的异步流程控制 | 前端进阶 2025-11-06
- JavaScript Proxy与Reflect深度实战:构建智能数据拦截系统 | 前端高级技巧 2025-11-06
- JavaScript可视化编程实战:从零构建WebGL数据可视化引擎完整指南 2025-11-02
- JavaScript Proxy与Reflect高级应用:构建智能数据响应系统实战 | 原创教程 2025-11-01
- JavaScript Generator函数与协程编程:异步流程控制的革命性解决方案 2025-10-31
- JavaScript Web Workers多线程编程实战:提升前端应用性能的完整指南 2025-10-29
- JavaScript数据可视化实战:使用Canvas构建动态粒子系统 | 前端开发教程 2025-10-29

