当前位置:网站首页>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 .
边栏推荐
- Detailed summary of flex layout
- Basic knowledge of web pages (URL related)
- Codeforces Round #802 (Div. 2) C D
- Create an environment for new projects
- OOP stack class template (template +ds)
- A summary of the experiment of continue and break in C language
- SQL lab range explanation
- 两小时带你进入软件测试行业风口(附全套软件测试学习路线)
- Google Earth engine (GEE) - Global jrc/gsw1_ 1 / batch download of yearlyhistory dataset (China region)
- The print area becomes smaller after epplus copies the template
猜你喜欢

Two hours to take you into the software testing industry (with a full set of software testing learning routes)

固態硬盤開盤數據恢複的方法

API interface management setup -eolinker4.0

台式电脑连不上wifi怎么办

Teach you to write non maintainable PHP code step by step

Prototypical Networks for Few-shot Learning

Various pits encountered in the configuration of yolov3 on win10

SOC验证环境的启动方式

Summary of SQL injection (I)

【FLink】access closed classloader classloader. check-leaked-classloader
随机推荐
基于SSH实现的学生成绩管理系统
Ranorex Studio 10.1 Crack
Personalized Federated Learning with Moreau Envelopes
Five simple data types of JS
How to open the DWG file of the computer
Various pits encountered in the configuration of yolov3 on win10
Mobile number regular expression input box loses focus verification
Create an environment for new projects
parallel recovery slave next change & parallel recovery push change
SRC platform summary
How to install the blue lake plug-in to support Photoshop CC 2017
Attack and defense world web baby Web
Abuse unlimited authorization -- is your address safe?
JDBC (IV)
Get to know the drawing component of flutter - custompaint
Student achievement management system based on SSH
Detailed summary of position positioning
API interface management setup -eolinker4.0
Using JS to realize the sidebar of life information network
How micro engine uploads remote attachments