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

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.

主站蜘蛛池模板: 马鞍山市| 曲麻莱县| 洛南县| 天台县| 墨江| 林芝县| 阳谷县| 泰来县| 邻水| 富平县| 三门县| 商河县| 毕节市| 互助| 罗山县| 大宁县| 黎川县| 理塘县| 长子县| 修水县| 婺源县| 浙江省| 治县。| 金川县| 泰顺县| 六安市| 永修县| 保定市| 栾城县| 逊克县| 平山县| 临泽县| 浦北县| 咸宁市| 漳州市| 梨树县| 兰州市| 咸丰县| 博客| 南昌县| 兴文县|