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

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.

主站蜘蛛池模板: 鸡泽县| 舟曲县| 云梦县| 广宁县| 大名县| 财经| 波密县| 财经| 当雄县| 湾仔区| 伊宁县| 梅州市| 兴安盟| 白玉县| 古田县| 元朗区| 德保县| 正蓝旗| 陆丰市| 赞皇县| 双江| 山阳县| 珲春市| 金昌市| 江陵县| 禹城市| 阿合奇县| 兴国县| 会理县| 阜宁县| 广元市| 刚察县| 广宗县| 大田县| 衡阳县| 清水县| 五家渠市| 长子县| 阿拉善右旗| 年辖:市辖区| 宜君县|