当前位置:网站首页>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 .
边栏推荐
- There is 404 in the laravel visit, except the home page is redirected; Index php
- Basic knowledge of web pages (URL related)
- dotnet-exec 0.4.0 released
- Fun CMD command line~
- "Daily practice, happy water" 1108 IP address invalidation
- parallel recovery slave next change & parallel recovery push change
- Go deep into the working principle of browser and JS engine (V8 engine as an example)
- Qdebug June 2022
- Cookie & session & JSP (XII)
- Google Earth Engine(GEE)——全球JRC/GSW1_1/YearlyHistory数据集的批量下载(中国区域)
猜你喜欢

Prototypical Networks for Few-shot Learning

Student achievement management system based on SSH

MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code

基于SSH实现的学生成绩管理系统

buuctf(pwn)

Startup mode of SoC verification environment

Penetration test - directory traversal vulnerability

本轮压力测试下,DeFi协议们表现如何?

SQL lab range explanation

固態硬盤開盤數據恢複的方法
随机推荐
Rce code execution & command execution (V)
Two hours to take you into the software testing industry (with a full set of software testing learning routes)
Mysql interactive_ Timeout and wait_ Timeout differences
How to make colleagues under the same LAN connect to their own MySQL database
Teach you to write non maintainable PHP code step by step
Ranorex Studio 10.1 Crack
Implementation of websocket long connection by workman under laravel
渗透测试-目录遍历漏洞
buuctf(pwn)
Penetration test - directory traversal vulnerability
SRC platform summary
CTFHUB SSRF
Vscade setting clang format
ThinkPHP 5 log management
Laravel Aurora push
Working principle of asemi three-phase rectifier bridge
For in JS Of and for in
Visual studio 2022 interface beautification tutorial
XSS (cross site script attack) summary (II)
API interface management setup -eolinker4.0