- 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!
- PostgreSQL Cookbook
- 網頁設計與制作教程(HTML+CSS+JavaScript)(第2版)
- 編譯系統透視:圖解編譯原理
- The DevOps 2.4 Toolkit
- Unreal Engine 4 Shaders and Effects Cookbook
- Visual C++開發入行真功夫
- WordPress 4.0 Site Blueprints(Second Edition)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- 快速入門與進階:Creo 4·0全實例精講
- Kotlin Programming By Example
- Natural Language Processing with Python Quick Start Guide
- 深入實踐DDD:以DSL驅動復雜軟件開發
- Robot Framework Test Automation
- 精通Spring:Java Web開發與Spring Boot高級功能
- Dart:Scalable Application Development