- 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?
推薦閱讀
- Embedded Linux Projects Using Yocto Project Cookbook
- SQL Server 從入門到項目實踐(超值版)
- R語言數(shù)據(jù)分析從入門到精通
- OpenDaylight Cookbook
- C程序設(shè)計簡明教程(第二版)
- 跟小海龜學(xué)Python
- NumPy Essentials
- Podman實戰(zhàn)
- Microsoft Dynamics GP 2013 Reporting, Second Edition
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- KnockoutJS Starter
- SQL Server從入門到精通(第3版)
- 焊接機器人系統(tǒng)操作、編程與維護
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- C++ Fundamentals