- Mastering JavaScript Functional Programming
- Federico Kereki
- 148字
- 2021-07-02 22:41:10
Solution #6 - redefine the handler
Another idea: instead of changing anything in the button, let's have the event handler change itself. The trick is in the second line; by assigning a new value to the billTheUser variable, we are actually dynamically changing what the function does! The first time you call the function, it will do its thing... but it will also change itself out of existence, by giving its name to a new function:
function billTheUser(some, sales, data) {
billTheUser = function() {};
window.alert("Billing the user...");
// actually bill the user
}
There's a special trick in the solution. Functions are global, so the line billTheUser=... actually changes the function's inner workings; from that point on, billTheUser will be the new (null) function. This solution is still hard to test. Even worse, how would you restore the functionality of billTheUser, setting it back to its original objective?
推薦閱讀
- R語言經典實例(原書第2版)
- VMware vSphere 6.7虛擬化架構實戰指南
- The Computer Vision Workshop
- Python Data Analysis(Second Edition)
- 小程序,巧運營:微信小程序運營招式大全
- MongoDB權威指南(第3版)
- Spring Boot企業級項目開發實戰
- Getting Started with Gulp
- Web Development with MongoDB and Node(Third Edition)
- Spring技術內幕:深入解析Spring架構與設計原理(第2版)
- 軟件體系結構
- Elasticsearch Essentials
- Android應用開發實戰
- 遠方:兩位持續創業者的點滴思考
- Tkinter GUI Programming by Example