- 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.
- 高校網絡道德教育研究
- Learning QGIS 2.0
- 新一代物聯網架構技術:分層算力網絡
- 物聯網概論(第2版)
- 中小型局域網組建、管理與維護實戰
- React:Cross-Platform Application Development with React Native
- 通信十年:擁抱互聯網
- Getting Started with nopCommerce
- 異構蜂窩網絡關鍵理論與技術
- AWS Lambda Quick Start Guide
- Guide to NoSQL with Azure Cosmos DB
- Professional Scala
- 信息技術安全評估準則:源流、方法與實踐
- NB-IoT原理和優化
- 物聯網傳感器技術與應用