当前位置:网站首页>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);
边栏推荐
- 【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
- 15. Using the text editing tool VIM
- There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
- 【兰州大学】考研初试复试资料分享
- Nacos conformance protocol cp/ap/jraft/distro protocol
- jacoco代码覆盖率
- 微信小程序 01
- Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
- [server data recovery] a case of RAID data recovery of a brand StorageWorks server
- Cocos uses custom material to display problems
猜你喜欢
Monthly observation of internet medical field in May 2022
How to release NFT in batches in opensea (rinkeby test network)
MongoD管理数据库的方法介绍
#HPDC智能基座人才发展峰会随笔
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
What is Base64?
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
unnamed prototyped parameters not allowed when body is present
Keil5 does not support online simulation of STM32 F0 series
随机推荐
Using eating in cocos Creator
避坑:Sql中 in 和not in中有null值的情况说明
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
Vertex shader to slice shader procedure, varying variable
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
./ Functions of configure, make and make install
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
Yunxiaoduo software internal test distribution test platform description document
MySQL bit type resolution
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
Annexb and avcc are two methods of data segmentation in decoding
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
jacoco代码覆盖率
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Whole process analysis of unity3d rendering pipeline
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
The rebound problem of using Scrollview in cocos Creator