[Functional Programming] Counter example
作者:互联网
document.getElementById("app").innerHTML = ` <h1>Hello Parcel!</h1> <div> <button id="plus">+</button> <button id="mius">-</button> </div> `; const addListener = (selector, eventType) => (listener) => { const element = document.querySelector(selector); element.addEventListener(eventType, listener); }; const plusClick = addListener("#plus", "click"); const miusClick = addListener("#mius", "click"); const hardCode = (newValue) => (broadcaster) => (listener) => { return broadcaster((value) => { listener(newValue); }); }; const plusOne = hardCode(1)(plusClick) const miusOne = hardCode(-1)(miusClick) const add = (initial) => broadcaster => listener => { return broadcaster((value) => { listener(initial += value) }) } const merge = (b1, b2) => listener => { b1(listener) b2(listener) } const counter = add(0)(merge(plusOne, miusOne)) counter(console.log)
标签:addListener,const,hardCode,Counter,Programming,Functional,value,listener,broadca 来源: https://www.cnblogs.com/Answer1215/p/13894510.html