How to implement native map in js

王林
Release: 2020-03-26 10:12:30
forward
2782 people have browsed it

How to implement native map in js

JS native method map implementation, the code is as follows:

       map 
Copy after login

(Recommended tutorial:js tutorial)

Supplementary knowledge points :

We usually use the encapsulated map method. If we encapsulate a map ourselves, how should we implement it.

The basic principle remains unchanged. In fact, the core of traversing an array is the for loop. Therefore, a map method is encapsulated below.

The ideas are as follows:

1. Add a method to the prototype

2. Pass a function and this

3. The parameters passed by the call method are the same as those of the encapsulated map method.

Array.prototype.fakeMap = function(fn,context) { let arr = this; let temp = []; for(let i=0;i
        
Copy after login

Recommended video tutorial:javascript video tutorial

The above is the detailed content of How to implement native map in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!