JS native method map implementation, the code is as follows:
(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!