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

边栏推荐
- Shader Language
- Virtual memory, physical memory /ram what
- Getting started with webgl (2)
- How to create Apple Developer personal account P8 certificate
- 【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
- Getting started with webgl (1)
- The significance of XOR in embedded C language
- Wechat applet 01
- HW初级流量监控,到底该怎么做
- After UE4 is packaged, mesh has no material problem
猜你喜欢

Streaming end, server end, player end

Typescript release 4.8 beta

Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base

Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
![[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)](/img/e9/9e32e38e12e1fa71732c52b8ee0ab0.png)
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)

15. Using the text editing tool VIM

Whole process analysis of unity3d rendering pipeline

2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码

Syntax of generator function (state machine)

Ue4/ue5 multi thread development attachment plug-in download address
随机推荐
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
大表delete删数据导致数据库异常解决
摘抄的只言片语
webgl_ Graphic transformation (rotation, translation, zoom)
After UE4 is packaged, mesh has no material problem
How to release NFT in batches in opensea (rinkeby test network)
webgl_ Enter the three-dimensional world (2)
使用cpolar建立一个商业网站(2)
How to deploy the super signature distribution platform system?
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
numpy--数据清洗
[server data recovery] data recovery case of raid failure of a Dell server
2. Basic knowledge of golang
Getting started with webgl (2)
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
【原创】一切不谈考核的管理都是扯淡!
一大波开源小抄来袭
STM32F103C8T6 PWM驱动舵机(SG90)
Database exception resolution caused by large table delete data deletion
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address