当前位置:网站首页>Understand JS high-order function and write a high-order function
Understand JS high-order function and write a high-order function
2022-06-25 05:05:00 【I am Feng Feng Yi】
If you are learning JavaScript, So you should have seen the term higher order function . Although it sounds complicated , But it's not hard . send JavaScript The reason for functional programming is that it accepts higher-order functions . Higher order functions are in JavaScript Widely used in . If you already use JavaScript Programmed for a while , You may have used them before you know it .
Misunderstanding of higher order function : Higher order functions do not mean that parameters have function parameters , Then the return value is the function of the function .
I understand it : Higher order functions refer to formal parameters with function parameters , And this higher-order function uses function parameters to do some things , Including processing some logic or encapsulating it into another function to return .
stay JS There are many higher-order functions in , Especially arrays
For example , You'll see .Array.prototype.map
Let's first look at method calls
const arr = [1,2,3];
const res = arr.map(it=>it*it);
// res [1,4,9];
Another simple simulation map
Array.prototype.map = function (cb,_this) {
if(!Array.isArray(this) || typeof cb !== 'function') {
return
}
const map = [];
const arr = this;
for(let i = 0; i < arr.length; i ++) {
map.push(cb.call(_this,arr[i],i,arr));
}
return map;
}
In this simulation Array.prototype.map The function of , We passed a function parameter , And use this function parameter to do some things , This map A function is a higher order function .
perhaps
We encapsulate a function and return a new function
const hof = function (func) {
return function (...args) {
Here are some things to deal with
func.apply(this,args);
Encapsulating functions
}
}
there hof Function is also a higher-order function , It encapsulates the passed in function parameters , Enhanced the function .
Common anti shake at the front end , Throttling and coriolism are also classic examples of higher-order functions , Higher order functions can improve the readability of our code , It can also improve the extensibility of our code , You can encapsulate existing functions , Enhance its function .
边栏推荐
- JDBC (IV)
- Two hours to take you into the software testing industry (with a full set of software testing learning routes)
- DOM document object model (I)
- CTFHUB SSRF
- Filter & listener (XIV)
- How PHP gets the user's City
- dotnet-exec 0.4.0 released
- Array: force deduction dichotomy
- 《QDebug 2022年6月》
- Specific operations for uploading pictures in PHP
猜你喜欢

Go deep into the working principle of browser and JS engine (V8 engine as an example)

Flex flexible layout for mobile terminal page production

My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced

Detailed summary of flex layout

Activereportsjs V3.0 comes on stage

The SQL response is slow. What are your troubleshooting ideas?

Detailed summary of float

Various pits encountered in the configuration of yolov3 on win10

DOM document object model (I)

Route parameters to jump to the page and transfer parameters -- > hidden parameter list
随机推荐
Redis (17)
Eyeshot Ultimate 2022 Crack By Xacker
parallel recovery slave next change & parallel recovery push change
Use serialize in egg to read and write split tables
Construction scheme of distributed websocket
Integrate CDN to create the ultimate service experience for customers!
Flex flexible layout for mobile terminal page production
OLAP analysis engine kylin4.0
win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法
电脑的dwg文件怎么打开
Filter & listener (XIV)
【FLink】access closed classloader classloader. check-leaked-classloader
JDBC (IV)
[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]
Activereportsjs V3.0 comes on stage
Specific operations for uploading pictures in PHP
固態硬盤開盤數據恢複的方法
TeeChart Pro ActiveX 2022.1
Detailed summary of float
Cookie & session & JSP (XII)