- 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!
- Microsoft Dynamics 365 Extensions Cookbook
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- 實戰低代碼
- QGIS:Becoming a GIS Power User
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- MATLAB 2020從入門到精通
- Android Wear Projects
- Python深度學習原理、算法與案例
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- 并行編程方法與優化實踐
- Drupal 8 Development Cookbook(Second Edition)
- Vue.js 3.x高效前端開發(視頻教學版)
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰
- Implementing Domain:Specific Languages with Xtext and Xtend
- 接口自動化測試持續集成:Postman+Newman+Git+Jenkins+釘釘