- 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.
- 通信網(wǎng)絡(luò)基礎(chǔ)與設(shè)備
- HCNA網(wǎng)絡(luò)技術(shù)
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡(luò)實用技術(shù)速成
- 互聯(lián)網(wǎng)安全的40個智慧洞見:2015年中國互聯(lián)網(wǎng)安全大會文集
- Socket.IO Real-time Web Application Development
- 計算機網(wǎng)絡(luò)原理與應(yīng)用技術(shù)
- 雷達饋線技術(shù)
- 物聯(lián)網(wǎng)之霧:基于霧計算的智能硬件快速反應(yīng)與安全控制
- Unity Artificial Intelligence Programming
- 互聯(lián)網(wǎng)+思維與創(chuàng)新:通往未來的+號
- 網(wǎng)絡(luò)工程實施技術(shù)與方案大全
- 互聯(lián)網(wǎng)安全的40個智慧洞見(2016)
- TCP/IP基礎(chǔ)(第2版)
- 基于IPv6的家居物聯(lián)網(wǎng)開發(fā)與應(yīng)用技術(shù)
- 想象的互動:網(wǎng)絡(luò)人際傳播中的印象形成