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

The publisher-subscriber model

// Publish and subscribe

class EventEmitter{
  constructor(){
    this.subs = Object.create(null)
  }

  $on(eventType, handler){
    this.subs[eventType] = this.subs[eventType] || []
    this.subs[eventType].push(handler)
  }

  $emit(eventType, params){
    if (this.subs[eventType]) {
      this.subs[eventType].forEach(handler= > {
        handler(params)
      })
    }
  }
}

let em = new EventEmitter()
/ / subscribe
em.$on('click'.(params) = >{
  console.log('click', params);
})
em.$on('click'.(params) = >{
  console.log('click', params);
})

/ / release
em.$emit('click'.1111111)
Copy the code

Observer model

// Observer mode


class Subject{ // 1. Observed
  constructor(name){
    this.name
    this.observers = []
    this.state = 'happy'
  }
  attach(o){
    this.observers.push(o)
  }
  setState(newState){
    if (this.state ! == newState) {this.state = newState
      this.notify(newState)
    }
  }
  notify(newState){
    this.observers.forEach(o= > o.update(newState))
  }

}



class Observer{
  constructor(name){
    this.name = name
  }
  update(state){
    console.log(`The ${this.name}: The current status is${state}`); }}// There is a baby in my family. Parents should pay attention to the status of the baby bag. If the baby is unhappy, it will inform the observer
let s = new Subject('baby');
let o1 = new Observer('daddy');
let o2 = new Observer('mother');
s.attach(o1)
s.attach(o2)
s.setState('Unhappy');
s.setState('happy');
Copy the code

Promise.all

Promise.all = function all(promises){
  if(!Array.isArray(promises)) throw new TypeError(promises + 'must be an Array')
  var n = 0,result = []
  return new Promise(function (resolve, reject){
    for(var i = 0; i < promises.length; i++){
      (function(i){
        var promise = promises[i]
        if(! isPromise(promise)) promise =Promise.resolve(promise)
        promise.then(function onfullfilled(value){
          n++;
          result[i] = value
          if( n >= promise.length) resolve(result)
        }).catch(function onrejected(reason){
          reject(reason)
        })
      })(i)
    }
  })
}
Copy the code

Detect whether pure object Class is directly under the Object | | Object. The create (null)

/ / testing whether pure object Class is directly under the Object | | Object. The create (null)
const isPlainObject = function isPlainObject(target) {
  let proto,/ / the prototype of the target
    Ctor
  if(! target ||Object.prototype.toString.call(target) ! = ='[object Object]') return false
  proto = Object.getPrototypeOf(target)
  if(! proto)return true
  Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor
  let ObjectFunctionString = Function.prototype.toString.call(Object); //"function Object() { [native code] }"
  return typeof Ctor === "function" && Function.prototype.toString.call(Ctor) === ObjectFunctionString
}
Copy the code