1. promise.all

Promise.all = function(promises{

  return new Promise(function(resolve, reject{

    var result = []

    var count = 0

    var promiseNum = promises.length

    if (promises.length === 0) resolve([])



    for (let i = 0; i < promiseNum; i++) {

      Promise.resolve(promises[i]).then(function(val{

        count++

        result[i] = val

        if (count === promiseNum) resolve(result)

      }, function(error){

        reject(error)

      })

    }

  })

}

Copy the code

2. promise.race

Promise.race = function(promises{

  return new Promise(function(resolve, reject){

    for (var i; i < promises.length; i++) {

      Promise.resolve(promises[i]).then(function(data){

        resolve(data)

      }, function(error{

        reject(error)

      })

    }

  })

}

Copy the code

3. Object.create

_create = function(proto, properties{

  // declare a new function

  var F = function({}

  // Point the function's prototype to proto

  F.prototype = proto

  // Return the power object of this function

  var o = new F()

  if (typeof properties === 'object') {

    Object.defineProperties(o, properties)

  }

  return o

}

Copy the code

4. new

function _new(Con, ... args{

  //1. Create an empty simple JavaScript object (car = {});

  let obj = {}

  // 2. Assign the constructor's scope to the new object (so this refers to the object);

  Object.setPrototypeOf(obj, Con.prototype)

  // obj.constructor.prototype === obj.__proto__ === Con.prototype

  // 3. Call the Car constructor and get the result (execute the code in the constructor (add attributes to the new object))

  let result = Con.apply(obj, args)



  // 4. If the constructor returns an object, use the constructor as the return value; Otherwise, return the temporary object (that is, this), whose prototype chain points to the constructor

  return result instanceof Object ? result : obj

}

Copy the code

5. map

Array.prototype.map = function(fn{

  if(this= = =null || undefined){

    throw 'Input cannot be null or underfinded'

  }

  

  let res = []

  

  for (let i=0; i<this.length; i++) {

    res.push(fn(this[i], i, this))

  }



  return res

}

Copy the code

6. filter

Array.prototype.filter = function(fn{

  if(this= = =null || undefined){

    throw 'Input cannot be null or underfinded'

  }



  let res = []



  for (let i = 0; i <this.length; i++) {

    if (fn(this[i],i,this)) {

      res.push(this[i])

    }

  }



  return res

}

Copy the code

7. reduce

Array.prototype.reduce = function(fn, base{

  let initialArr = this;

  let arr = initialArr.concat();

  if(base) arr.unshift(base)

  let newValue;



  while(arr.length > 1) {

    newValue = fn.call(null, arr[0], arr[1]);

    arr.splice(0.2,newValue)

  }



  return newValue

}

Copy the code

8. A deep copy

function deepCopy(obj, cache = new Map()) {

  if (cache.get(obj)) {

    return cache.get(obj)

  }



  if (typeofobj ! = ='object'return obj

  if (typeof obj === nullreturn obj

  let cloneObj



  if (obj instanceof Datereturn new Date(obj.getTime())

  if (obj instanceof RegExpreturn new RegExp(obj.source, obj.flags)

  if (obj instanceof Array) {

    cloneObj = []

  } else {

    const p = obj.constructor.prototype

    cloneObj = Object.create(p) // Copy the prototype chain of the Object as well. If {} is used instead of p, Object constructors will all be Object

  }

  cache.set(obj)

  for (let key in obj) {

    // Clone only the property, not its prototype property

    if (obj.hasOwnProperty(key)) {

      cloneObj[key] = deepCopy(obj[key], cache)

    }

  }

  return cloneObj

}

Copy the code

9. If you

function debounce(fn, delay{

  let ts = null



  return function({

    clearTimeout(ts)

    ts = setTimeout((a)= >  {

      fn.apply(this.arguments// It does not matter whether apply is used in this example

    }, delay)

  }

}

Copy the code

10. The throttle

function throttle(fn, delay{

  let ts = null

  return function({

    if(! ts) {

      ts = setTimeout((a)= > {

        fn.call(this. arguments)

        ts = null

      }, delay)

    }

  }

}

Copy the code

11. call

Function.prototype.call = function(context{

  if(typeof this! = ='function') {

    throw new TypeError(`The ${this} is not a function`)

  }



  context = context || window;

  context.fn = this

  const args = [...arguments].slice(1)

  constresult = context.fn(... args)

  delete context.fn



  return result

}

Copy the code

12. apply

Function.prototype.apply = function(context{

  if (typeof this! = ='function') {

    throw new TypeError(`The ${this} is not a function`)

  }



  context = context || window;

  context.fn = this



  let result

  if (arguments[1]) {

result = context.fn(... arguments[1])

  } else {

    result = context.fn()

  }



  delete context.fn

  return result

}

Copy the code

13. bind

Function.prototype.bind = function(context{

  if (typeof this! = ='function') {

    throw new TypeError(`The ${this} is not a function`)

  }

  context = context || window



  context.fn = this

  const args = [...arguments].slice(1)



  return function F({

    // If new is called

    if (this instanceof F) {

      return newcontext.fn(... args, ... arguments)

    }



    returncontext.fn(args.concat(... arguments))

  }

}

Copy the code

  • If you find it useful, give the author a thumbs up.
  • There must be a lot of improvement in the code, welcome to exchange.
  • Welcome to follow the wechat public account: [Front-end xiaocard].