当前位置:网站首页>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);

边栏推荐
- 【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
- [Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
- 【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
- [quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
- Wechat applet 01
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- Syntax of generator function (state machine)
- [quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
- ./ Functions of configure, make and make install
- Mathematical modeling -- what is mathematical modeling
猜你喜欢

Syntax of generator function (state machine)

Asynchronous application of generator function

MySQL bit type resolution

Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)

Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
![[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)

【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)

Keil5 does not support online simulation of STM32 F0 series
![[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)](/img/a3/7f08f189c608d6086b368dfa3831f7.png)
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
![[deep learning] image hyperspectral experiment: srcnn/fsrcnn](/img/84/114fc8f0875b82cc824e6400bcb06f.png)
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
随机推荐
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
C Alibaba cloud OSS file upload, download and other operations (unity is available)
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
OpenGL common functions
Keil5 does not support online simulation of STM32 F0 series
Async and await
2.Golang基础知识
Basic knowledge sorting of mongodb database
AB package details in unity (super detail, features, packaging, loading, manager)
How to build your own super signature system (yunxiaoduo)?
The download button and debug button in keil are grayed out
微信小程序 01
从 1.5 开始搭建一个微服务框架链路追踪 traceId
Detailed explanation of Cocos creator 2.4.0 rendering process
Tkinter after how to refresh data and cancel refreshing
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Database exception resolution caused by large table delete data deletion
webgl_ Enter the three-dimensional world (2)
Oracle控制文件丢失恢复归档模式方法
[wechat applet] Chapter (5): basic API interface of wechat applet