- Mastering JavaScript Functional Programming
- Federico Kereki
- 206字
- 2021-07-02 22:41:10
Solution #7- use a local flag
We can go back to the idea of using a flag, but instead of making it global (which was our main objection) we can use a Immediately Invoked Function Expression (IIFE): we'll see more on this in Chapter 3, Starting Out with Functions - A Core Concept, and in Chapter 11, Implementing Design Patterns - The Functional Way. With this, we can use a closure, so clicked will be local to the function, and not visible anywhere else:
var billTheUser = (clicked => {
return (some, sales, data) => {
if (!clicked) {
clicked = true;
window.alert("Billing the user...");
// actually bill the user
}
};
})(false);
See how clicked gets its initial false value, from the call at the end.
This solution is along the lines of the global variable solution, but using a private, local variable is an enhancement. About the only objection we could find, is that you'll have to rework every function that needs to be called only once, to work in this fashion. (And, as we'll see in the following section, our FP solution is similar in some ways to it.) OK, it's not too hard to do, but don't forget the Don't Repeat Yourself (D.R.Y) advice!
- Mastering Concurrency Programming with Java 8
- 表哥的Access入門:以Excel視角快速學習數據庫開發(第2版)
- 可解釋機器學習:模型、方法與實踐
- 數據結構習題解析與實驗指導
- Linux C編程:一站式學習
- Unity&VR游戲美術設計實戰
- Spring MVC+MyBatis開發從入門到項目實踐(超值版)
- Beginning C++ Game Programming
- JavaScript+jQuery網頁特效設計任務驅動教程
- ROS機器人編程實戰
- Get Your Hands Dirty on Clean Architecture
- Beginning C# 7 Hands-On:The Core Language
- Java程序設計(項目教學版)
- Building Microservices with .NET Core 2.0(Second Edition)
- Mastering WooCommerce 4