JS first understanding of high-order functions and function currying

1. Higher-order functions

basic concept

Satisfies one of the following conditions, that is, a higher-order function

  • A parameter that a function receives is also a function.
  • A function returns a function.

simple example

Let me give you an example:
the following fun and fun1 functions can both be called higher-order functions.

function fun(callback){
    
    
	console.log('执行fun函数')
	callback()
}
function back(){
    
    
	console.log('执行back函数')
}
fun(back) //接收一个函数作为参数


function fun1(){
    
    
	console.log('执行fun函数')
	return function back1(){
    
    
	console.log('执行back函数')
			}
}
console.log(fun1()()) //调用该fun1函数时会返回一个函数

Seeing this, I believe everyone already understands what a high-order function is. Isn't it a very simple thing? Then let's take a look at which functions are high-order functions among our commonly used functions.

Common higher-order functions

Promise, timer (setTimeout, setInterval), array method (map, some...), etc.


Higher-order functions are very simple, so let's take a look at what function currying means?

function currying

basic concept

  • Through the way of function calling and continuing to return functions, the function form of receiving parameters multiple times and finally processing them in a unified manner is realized.

Through this concept, you may not understand it very well, so the old routine, let’s look at the example and it will be very simple.
Just like the tool man uses Zhang San, since it is an example tool function, let’s use summation

//普通函数求和
function sum(x,y,z){
    
    
	return (x + y + z);
}
console.log(sum(1,2,3)) //这是普通函数求和的方式

//函数柯里化求和
function sum(x){
    
    
	return(y)=>{
    
    
		return (z)=>{
    
    
			return x + y + z
	}
		}
}
console.log(sum(1)(2)(3)) //通过不断调用返回函数的方式最后统一处理得到的值

After reading the function currying in the example, I don’t know if you will think of a method that is often used in a function: recursion
is also through calling the function continuously, and finally processing the returned function value uniformly.

at last

Finally, let me make a summary here: the definition of
a high-order function is to see whether the acceptance and return of our function is a function , and the currying of the function mainly depends on the continuous return and call of our function . To put it bluntly, look at the following function Parentheses (of course, this view is limited to individuals, welcome to criticize and correct), personally feel that recursion is also a shining example of function currying.


Higher-order functions and function initialization may be an ignorant concept for beginners, and they may even think it is something advanced. But after reading this blog post after having a little bit of functional foundation, I believe you can clearly understand that these things are very simple. That's all for this sharing, thank you all

Guess you like

Origin blog.csdn.net/weixin_51033461/article/details/126097644