Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

New operator, instanceof operator, function corrification, function combination

New operator

    const _new = function _new(Ctor,... params){
      // Format check: function & has prototype object & not Symbol/BigInt
      if(typeofCtor ! = ='function') throw new TypeError(`${Ctor} is not a constructor`)
      let name = Ctor.name,
          proto = Ctor.prototype
      if(/^(Symbol|BigInt)$/i.test(name) || ! proto)throw new TypeError(`${name} is not a constructor`)
      // Create an instance object of the current class
      let obj = Object.create(Ctor.prototype)

      // Execute the constructor like a normal function, but this needs to point to the instance object being created
      letresult = Ctor.call(obj,... params)If no value is written, or if the original value is returned, we return the real column object by default. If an object is returned, the object is returned by itself
      if(result ! = =null && /^(object|function)$/i.test(typeof result)) return result
      return obj
    }
Copy the code

The instanceof operator

  • You can refer to the article to learn about the principles of Instanceof: four methods of data type detection and their principles
    const instance_of = function instance_of(obj, Ctor) {
      if(Ctor == null) throw new TypeError('Right-hand side of instanceof is not a object')
      if (typeofCtor ! = ='function') throw new TypeError('Right-hand side of instanceof is not callable')
      if(! Ctor.prototype)throw new Error('Function has non-object prototype undefined in instanceof check')

      if(obj == null || !/^(object|function)$/.test(typeof obj)) return false

      if(typeof Ctor[Symbol.hasInstance] === 'function') return Ctor[Symbol.hasInstance](obj)

      let proto = Object.getPrototypeOf(obj)
      while (proto) {
        if(proto === Ctor.prototype) return true
        proto = Object.getPrototypeOf(proto)
      }
      return false
    }
Copy the code

The function is currified

Prestorage (preprocessing), using the closure “save mechanism “, we store some values in advance for use in their lower contexts.

    const curry = function curry(func){
      if(typeoffunc ! = ='function') throw new TypeError(`${func} is not a function`)

      return function curried(. params) {
        if (params.length < func.length) {
          return function(. args){
            returncurried(... params.concat(args)) } }returnfunc(... params) } }Copy the code

Function composition

If a function should be treated with multiple functions can get final value (such as h (g) (f (x))), the time can combine the functions of intermediate process into a function, function as data pipeline, function composition is to connect these pipes, let data through multiple pipes to form the final result, function composition by default from right to left.

    const compose = function compose(. funcs){
      let len = funcs.length
      if(len === 0) x => x
      if(len === 1) return funcs[0]
      return function operate(x){
        return funcs.reduceRight((memo, func) = > {
          return func(memo)
        }, x)
      }
    }
Copy the code