- Mastering JavaScript Functional Programming
- Federico Kereki
- 172字
- 2021-07-02 22:41:11
A functional solution
Let's try to be more general: after all, requiring that some function or other be executed only once, isn't that outlandish, and may be required elsewhere! Let's lay down some principles:
- The original function (the one that may be called only once) should do that thing, and no other
- We don't want to modify the original function in any way
- We need to have a new function that will call the original one only once
- We want a general solution that we can apply to any number of original functions
The first principle listed previously is the single responsibility principle (the S in S.O.L.I.D.), which states that every function should be responsible over a single functionality. For more on S.O.L.I.D., check the article by Uncle Bob (Robert C. Martin, who wrote the five principles) at http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod.
Can we do it? Yes; and we'll write a higher-order function, which we'll be able to apply to any function, to produce a new function that will work only once. Let's see how!
推薦閱讀
- Mastering Kali Linux for Web Penetration Testing
- Spring實戰(zhàn)(第5版)
- INSTANT OpenNMS Starter
- 快人一步:系統(tǒng)性能提高之道
- C和C++游戲趣味編程
- Python High Performance Programming
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Spring+Spring MVC+MyBatis從零開始學(xué)
- ArcGIS for Desktop Cookbook
- Android應(yīng)用開發(fā)實戰(zhàn)
- Practical Predictive Analytics
- 會當(dāng)凌絕頂:Java開發(fā)修行實錄
- Java EE 8 and Angular
- Backbone.js Patterns and Best Practices
- Serverless工程實踐:從入門到進階