- 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?
推薦閱讀
- TensorFlow Lite移動端深度學習
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- Responsive Web Design by Example
- WebRTC技術詳解:從0到1構建多人視頻會議系統
- Python編程從0到1(視頻教學版)
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎卷)
- Scala Data Analysis Cookbook
- 細說Python編程:從入門到科學計算
- 深入實踐Kotlin元編程
- Python Interviews
- 從程序員角度學習數據庫技術(藍橋杯軟件大賽培訓教材-Java方向)
- Instant GLEW
- Spring Boot從入門到實戰
- Professional JavaScript
- 快速搞定Spring Boot+Vue全棧開發