1. forEach
Array.prototype.forEach = function(fn, thisArg) {
 var _this;
 if (typeoffn ! = ="function") {
  throw "Parameters must be functions.";
 }
 if (arguments.length > 1) {
  _this = thisArg;
 }
 if (!Array.isArray(arr)) {
  throw "You can only use forEach on arrays.";
 }
 for (let index = 0; index < arr.length; index++) { fn.call(_this, arr[index], index, arr); }};Copy the code

2.class

www.cnblogs.com/gg-qq/p/115…

  1. set
function Set(){
    var items = {}
    var length = 0;
    // Check whether the element exists
    this.has = function(val){
        return items.hasOwnProperty(val)
    }
    // Add operations
    this.add = function(val){
        if(!this.has(val)){
            items[val] = val;
            length++;
            return true;
        }
        return false;
    }
    // Delete operation
    this.remove = function(val){
        if(this.has(val)){
            delete items[val]
            length-=1;
            return true;                
        }
        return false;
    }
    / / remove
    this.clear = function(){
        items = {};
        length = 0
        return true
    }
    // Get the size
    this.size = function(){
        return length;
    }
    // Get attributes
    this.values = function(){
        return Object.keys(items); }}var set = new Set()
set.add(1); set.add(2); set.add(3); set.add('a'(HTTPS://blog.csdn.net/zdhui_fly/article/details/81118796
Copy the code

4.map

Function Mymap() {// constructor this.init(); } // create bucket (array), each position is an object, set the next property on each object, and initialize to NULL. Mymap.prototype.init = function () { this.tong = new Array(8); for (var i = 0; i < 8; i++) { this.tong[i] = new Object(); this.tong[i].next = null; }}; // Add data. Mymap.prototype.set = function (key, value) { var index = this.hash(key); Var TempBucket = this.tong[index]; If (tempbucket.next. Key == key) {// Overwrite the value of the property to be set if it already exists. TempBucket.next.value = value; return; } else {TempBucket = tempbucket.next; // Point to the next object. }} next = {// next is null, add object. key: key, value: value, next: null } }; Mymap.prototype.get = function (key) {var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.key == key){ return TempBucket.value; }else{ TempBucket = TempBucket.next; } } return undefined; Mymap.prototype.delete = function(key){var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.next.key == key){ TempBucket.next = TempBucket.next.next; return true; }else{ TempBucket = TempBucket.next; Mymap.prototype.has = function(key){var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.key == key){ return true; }else{ TempBucket = TempBucket.next; } } return false; Mymap.prototype.clear = function(){this.init();} mymap.prototype. clear = function(){this.init(); } // Make the set properties evenly distributed to each location so that no chain is too long. Mymap.prototype.hash = function (key) { var index = 0; if (typeof key == "string") { for (var i = 0; i < 3; i++) { index = index + isNaN(key.charCodeAt(i)) ? 0 : key.charCodeAt(i); } } else if (typeof key == 'object') { index = 0; } else if (typeof key == 'number') { index = isNaN(key) ? 7 : key; } else { index = 1; } return index % 8; } var map = new Mymap(); Set ('name',' ZWQ '); map.get('name'); map.has('name);Copy the code

5.reduce

Array.prototype.myRedece = function(reducer, initValue){ for (let i = 0; i < this.length; i++) { initValue = reducer(initValue, this[i], i, this) } return initValue } const res = arr.myRedece(function(val, Item) {return val + item}, 0) the original link: https://blog.csdn.net/m0_37068028/article/details/83866756Copy the code

6.instanceof

function instance_of(L, R) {
    var O = R.prototype; 
    L = L.__proto__;
    while (true) {    
        if (L === null)      
             return false;   
        if (O === L) 
             return true; L = L.__proto__; }}Copy the code

7. New creates a new object based on the constructor’s Prototype property; Pass this(the new object in the previous sentence) and the call parameters to the constructor, and execute; If the constructor does not manually return an object, the new object created in the first step is returned. If it does, the new object created in the first step is discarded and the manually returned object is returned.

es6 function (Parent, ... Rest) {// 1. Create a new object using the constructor's prototype property; let child = Object.create(Parent.prototype); // 2. Pass this and call parameters to the constructor to execute let res=Parent. Apply (child, rest); Return typeof res === 'object'? res : child; }; es5 function (Parent, ... Rest) {// 1. Create a new object using the constructor's prototype property; let child={} child._proto_=Parent.prototype // 2. Pass this and call parameters to the constructor to execute let res= parent.apply (child, rest); Return typeof res === 'object'? res : child; };Copy the code