当前位置:网站首页>JS array foreach source code parsing
JS array foreach source code parsing
2022-07-07 15:48:00 【Sam young】
// The original method
(method) Array<number>.forEach(callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any): void
- notice
forEachThe first parameter in iscallbackfn: The callback method , You can putvalue,index,listReturn all tocallbackfnMethod inside .
const list = ['a', 'b', 'c', 'd'];
// Be careful not to use => (fn) {...} How to write it , because this The object will not be found
// item: Return the value of each array
// index: Return the index corresponding to the array 0 1 2....
// array: Return the array itself of the loop
Array.prototype.newForEach = function (fn: (item: any, index: number,array: any[]) => void) {
console.log(this); // there this Will point to list - ['a', 'b', 'c', 'd']
const len = this.length;
for (let i = 0; i < len; i++) {
// Pass the element to the callback function
fn(this[i], i,this);
}
};
list.newForEach((item, index) => {
console.log(item, index);
});
// a 0 b 1 c 2 d 3
- notice
forEachThe second parameter in isthisArg: It means pointing to the callback functionthisPoint to
var array1 = ['a', 'b', 'c'];
var array2 = ['1','2','3'];
array1.forEach(function(currentValue, index, arr) {
console.log(currentValue, index, arr, this);
},array2);
# Output
// If forEach() Pass the thisArg Parameters , When called , It will be passed on to callback function , As it is this value . otherwise , Will be introduced into undefined As it is this value
> "a" 0 ["a", "b", "c"] ["1", "2", "3"]
> "b" 1 ["a", "b", "c"] ["1", "2", "3"]
> "c" 2 ["a", "b", "c"] ["1", "2", "3"]
- The final code
const list = ['a', 'b', 'c', 'd'];
const list2 = [1, 2, 3];
// Be careful not to use => (fn) {...} How to write it , because this The object will not be found
// item: Return the value of each array
// index: Return the index corresponding to the array 0 1 2....
// array: Return the array itself of the loop
Array.prototype.newForEach = function (
fn: (item: any, index: number, array: any[]) => void,
thisArg?: any,
) {
console.log(this); // there this Will point to list - ['a', 'b', 'c', 'd']
const len = this.length;
for (let i = 0; i < len; i++) {
// Pass the element to the callback function
if (thisArg) {
fn.call(thisArg, this[i], i, this);
} else {
fn(this[i], i, this);
}
}
};
list.newForEach(function (currentValue, index, arr) {
console.log(currentValue, index, arr, this);
}, list2);

边栏推荐
- 微信小程序 01
- Starting from 1.5, build a microservice framework link tracking traceid
- XMIND frame drawing tool
- Mathematical modeling -- what is mathematical modeling
- MySQL bit type resolution
- webgl_ Enter the three-dimensional world (1)
- 【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
- Vertex shader to slice shader procedure, varying variable
- Nacos conformance protocol cp/ap/jraft/distro protocol
- Whether runnable can be interrupted
猜你喜欢

Typescript release 4.8 beta

Async and await
![[Lanzhou University] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Lanzhou University] information sharing of postgraduate entrance examination and re examination

Briefly describe the working principle of kept
![[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering](/img/a4/7320f5d266308f6003cc27964e49f3.png)
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering

MySQL bit type resolution

如何在opensea批量发布NFT(Rinkeby测试网)

How to create Apple Developer personal account P8 certificate

LeetCode1_ Sum of two numbers

unnamed prototyped parameters not allowed when body is present
随机推荐
After UE4 is packaged, mesh has no material problem
MongoD管理数据库的方法介绍
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Getting started with webgl (2)
【兰州大学】考研初试复试资料分享
Webcodecs parameter settings -avc1.42e01e meaning
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Share the technical details of super signature system construction
Nacos conformance protocol cp/ap/jraft/distro protocol
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
[wechat applet] Chapter (5): basic API interface of wechat applet
Getting started with webgl (4)
Annexb and avcc are two methods of data segmentation in decoding
微信小程序 01
2. Heap sort "hard to understand sort"
How to release NFT in batches in opensea (rinkeby test network)