This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Continue to learn the core knowledge of NodeJS. At present, I study and practice nodeJS according to the simple and profound book. The main reason is that this book is very classic.

Here are some study notes and exercises for asynchronous programming, chapter 4 of the Nodejs book

The electronic address of the book

Functional programming

JavaScript is a first-class citizen of functions, so it is very free to call it directly, or as arguments and return values. Here’s how functional programming works:

Higher-order functions

A higher-order function is one that can take a function as an argument or as a return value.

Higher-order functions form a way to receive results in subsequent passing styles, shifting the focus to callback functions.

The sort method of an array is a typical high-order function. The parameter of sort method is a function. You can modify the contents of the function to customize the sort method

const arr = [1.1.2.2.3];
arr.sort((a, b) = > {
    return b - a;
})
Copy the code

Partial function usage

The explanation in the article is not easy to understand, but my understanding is that it is a way to create other functions from a factory function.

For example, to determine the type of a variable, we can write:

const isString = (obj) = > {
    return Object.prototype.toString.call(obj) === "[object String]";
}
const isFunction = (obj) = > {
    return Object.prototype.toString.call(obj) === "[object Function]";
}
Copy the code

However, if there are many similar decisions, the middle part will become redundant, and we can use a factory function isType to do this:

const isType = (type) = > {
    return (obj) = > {
        return Object.prototype.toString.call(obj) === `[object ${type}] `}}const isString = isType('String');
const isFunction = isType('Function');
Copy the code

We use isType as a factory function to batch create similar functions, isString, isFunction, etc., by assigning the value of type.

This way of creating a new custom function by specifying some of its parameters is called partial function.

This is the introduction to functional programming. Comments and likes are welcome