当前位置:网站首页>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 .
边栏推荐
- 《QDebug 2022年6月》
- buuctf(re)
- 固態硬盤開盤數據恢複的方法
- A brief talk on media inquiry
- CTFHub-rce
- Array: force deduction dichotomy
- Notes on non replacement elements in the line (padding, margin, and border)
- Méthode de récupération des données d'ouverture du disque dur à l'état solide
- Kotlin compose listens to the soft keyboard and clicks enter to submit the event
- Matlab notes
猜你喜欢
How to use the Magic pig system reinstallation master
olap分析引擎——Kylin4.0
API interface management setup -eolinker4.0
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
Go deep into the working principle of browser and JS engine (V8 engine as an example)
XSS (cross site script attack) summary (II)
What is Ethernet and how to connect the computer
Personalized Federated Learning with Moreau Envelopes
ASEMI三相整流桥的工作原理
How micro engine uploads remote attachments
随机推荐
Create an environment for new projects
JDBC (IV)
Web3 DAPP user experience best practices
Detailed summary of position positioning
Compatible with Internet Explorer
Using JS to realize the sidebar of life information network
小白一键重装官网下载使用方法
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
EL & JSTL (XIII)
OOP stack class template (template +ds)
Response (XI)
What if win11 Bluetooth fails to connect? Solution of win11 Bluetooth unable to connect
Deeply understand the characteristics of standard flow and off standard elements
What if the desktop computer is not connected to WiFi
Fun CMD command line~
Flex flexible layout for mobile terminal page production
Creation and use of MySQL index
XML (VIII)
Difference between asemi high power FET and triode
融合CDN,为客户打造极致服务体验!