当前位置:网站首页>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
- 大表delete删数据导致数据库异常解决
- Unity's ASE achieves full screen sand blowing effect
- 2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
- Getting started with webgl (4)
- 一大波开源小抄来袭
- 使用Scrapy框架爬取网页并保存到Mysql的实现
- Using eating in cocos Creator
- Oracle控制文件丢失恢复归档模式方法
- Gd32 F3 pin mapping problem SW interface cannot be burned
猜你喜欢
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
#HPDC智能基座人才发展峰会随笔
HPDC smart base Talent Development Summit essay
【搞船日记】【Shapr3D的STL格式转Gcode】
AB package details in unity (super detail, features, packaging, loading, manager)
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
【数字IC验证快速入门】26、SystemVerilog项目实践之AHB-SRAMC(6)(APB协议基本要点)
一大波开源小抄来袭
OpenGL's distinction and understanding of VAO, VBO and EBO
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
随机推荐
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
Share the technical details of super signature system construction
Jacobo code coverage
Do not use memset to clear floating-point numbers
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
Cut ffmpeg as needed, and use emscripten to compile and run
LeetCode2_ Add two numbers
Async and await
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
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
Starting from 1.5, build a microservice framework link tracking traceid
Yunxiaoduo software internal test distribution test platform description document
【微信小程序】Chapter(5):微信小程序基础API接口
How to release NFT in batches in opensea (rinkeby test network)
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
How to deploy the super signature distribution platform system?
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
numpy--疫情数据分析案例
Streaming end, server end, player end