- React Design Patterns and Best Practices(Second Edition)
- Carlos Santana Roldán
- 179字
- 2021-06-24 15:43:41
First-class objects
In JavaScript, functions are first-class objects, which means that they can be assigned to variables and passed as parameters to other functions.
This allows us to introduce the concept of higher-order functions (HoFs). HoFs are functions that take a function as a parameter, optionally some other parameters, and return a function. The returned function is usually enhanced with some special behaviors.
Let's look at a simple example where there is a function for adding two numbers that gets enhanced with a function that first logs all the parameters and then executes the original one:
const add = (x, y) => x + y;
const log = fn => (...args) => {
console.log(...args);
return fn(...args);
};
const logAdd = log(add);
This concept is pretty important to understand, because, in the React world, a common pattern is to use HoCs, to treat our components as functions, and to enhance them with common behaviors. We will see HoCs and other patterns in Chapter 4, Compose All the Things.
- 廣電5G從入門到精通
- Cisco OSPF命令與配置手冊
- Spring Boot 2.0 Projects
- 5G承載網網絡規劃與組網設計
- Oracle SOA Suite 11g Performance Tuning Cookbook
- 計算機網絡工程實用教程(第2版)
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- Yii Application Development Cookbook(Second Edition)
- 智慧光網絡:關鍵技術、應用實踐和未來演進
- 6G新技術 新網絡 新通信
- OMNeT++與網絡仿真
- TD-LTE無線網絡規劃與設計
- 深入理解計算機網絡
- Web用戶查詢日志挖掘與應用
- Getting Started with tmux