- Mastering JavaScript Functional Programming
- Federico Kereki
- 147字
- 2021-07-02 22:41:10
Solution #4 - change the handle
A variant to the previous solution would be not removing the click function, and rather assign a new one instead. We are using functions as first class objects here, when we assign the alreadyBilled() function to the click event:
function alreadyBilled() {
window.alert("Your billing process is running; don't click, please.");
}
function billTheUser(some, sales, data) {
document.getElementById("billButton").onclick = alreadyBilled;
window.alert("Billing the user...");
// actually bill the user
}
There's a good point to this solution: if the user clicks a second time, they'll get a warning not to do that, but they won't be billed again. (From the point of view of the user experience, it's better.) However, this solution still has the very same objections as the previous one (code coupled to the button, need to reset the handler, harder testing), so we won't consider it to be quite good anyway.
推薦閱讀
- Android Studio Essentials
- Hadoop+Spark大數據分析實戰
- C語言程序設計
- Hands-On Automation Testing with Java for Beginners
- Instant Lucene.NET
- Building Machine Learning Systems with Python(Second Edition)
- Spring MVC+MyBatis開發從入門到項目實踐(超值版)
- PostgreSQL Developer's Guide
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Visual C#(學習筆記)
- Python GUI設計tkinter菜鳥編程(增強版)
- 多接入邊緣計算實戰
- 匯編語言程序設計
- JSP程序設計與案例教程
- Learning ClojureScript