官术网_书友最值得收藏!

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 4Compose All the Things.

主站蜘蛛池模板: 锡林浩特市| 隆安县| 古田县| 和田市| 赣榆县| 云霄县| 揭东县| 新闻| 秦皇岛市| 安阳市| 盐城市| 平原县| 广昌县| 楚雄市| 玉林市| 茶陵县| 上杭县| 保定市| 百色市| 哈巴河县| 绿春县| 周宁县| 丰城市| 仲巴县| 孝昌县| 浦东新区| 云南省| 青河县| 武川县| 从化市| 富宁县| 武邑县| 尉氏县| 若羌县| 平乡县| 浦城县| 荣成市| 鄂托克前旗| 固原市| 辉南县| 湘乡市|