当前位置:网站首页>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
forEach
The first parameter in iscallbackfn
: The callback method , You can putvalue,index,list
Return all tocallbackfn
Method 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
forEach
The second parameter in isthisArg
: It means pointing to the callback functionthis
Point 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);
边栏推荐
- Create lib Library in keil and use lib Library
- Pit avoidance: description of null values in in and not in SQL
- [wechat applet] Chapter (5): basic API interface of wechat applet
- The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
- Spin animation of Cocos performance optimization
- Database exception resolution caused by large table delete data deletion
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- How to create Apple Developer personal account P8 certificate
- 写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
- Streaming end, server end, player end
猜你喜欢
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
使用cpolar建立一个商业网站(2)
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Introduction of mongod management database method
Gd32 F3 pin mapping problem SW interface cannot be burned
Write sequence frame animation with shader
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
webgl_ Enter the three-dimensional world (2)
LeetCode3_ Longest substring without duplicate characters
随机推荐
2.Golang基础知识
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Using eating in cocos Creator
Typescript release 4.8 beta
Unity之ASE实现全屏风沙效果
What are PV and UV? pv、uv
C Alibaba cloud OSS file upload, download and other operations (unity is available)
Excerpted words
MongoD管理数据库的方法介绍
Introduction of mongod management database method
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Do not use memset to clear floating-point numbers
jacoco代码覆盖率
Async and await
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
The difference between full-time graduate students and part-time graduate students!
Detailed explanation of Cocos creator 2.4.0 rendering process
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
Getting started with webgl (3)