美文网首页
js回调相关知识点(发布订阅)

js回调相关知识点(发布订阅)

作者: 我叫Aliya但是被占用了 | 来源:发表于2019-06-11 01:05 被阅读0次

Object.prototype.toString.call('str') => "[object String]"
概念:装饰器、函数柯里化、偏函数、after方法、发布订阅、观察者

装饰器

Function.prototype.beforeCall = function () {
  console.info('方法守卫')
  this()
}
let fn = function () {
  console.info('方法内容')
}
fn.beforeCall()

函数柯里化 & 偏函数

把接受多个参数的函数,变换成接受一个单一参数的函数,并且返回接受余下的参数而且返回结果的新函数
偏函数:入参变为多个

// 改造bind
var temp = {name: 'Lili'} 
var obj = {
  name: 'fn',
  fn: function (a, b, c) {
    console.info('方法内容', this.name, a, b, c)
  }
}
Function.prototype.mybind = function (_t) {
  let self = this
  return function () { self.call(_t, ...arguments) }
}
obj.fn.mybind(temp)(1, 2, 3)

after方法

被触发指定次数后执行

function after (times, fn) {
  let n = 1
  return function () {
    if (n == times) fn(...arguments)
    else n++
  }
}
let fn = function (a) {
  console.info('方法内容', a)
}
let newfn = after(3, fn)
newfn(1)
newfn(2)
newfn(3)

发布订阅

let fakeBus = function () {
  this.eventList = {}
}
fakeBus.prototype.$on = function (order, fn) {
  this.eventList[order]
    ? this.eventList[order].push(fn)
    : this.eventList[order] = [fn]
}
fakeBus.prototype.$emit = function (order, ...args) {
  if (this.eventList[order]) {
    this.eventList[order].forEach(fn => {
      fn(...args)
    });
  }
}
let bus = new fakeBus()
bus.$on('bit', msg => console.info(1, msg))
bus.$on('bit', msg => console.info(2, msg))
bus.$emit('bit', 'ouch')

观察者

发布与订阅间有关系

class Watcher {
  constructor (obj) {
    this.__event_list = {}
    this.__watch(obj)
  }

  __watch (obj, father_key) {
    father_key = father_key || ''
    Object.keys(obj).forEach(item => {
      if (typeof obj[item] == 'object') {
        this.__watch(obj[item], father_key + item + '.')
      }
      else this.__append_getset(obj, item, father_key + item)
    })
  }

  __append_getset (obj, key, order) {
    let self = this
    let temp = obj[key]
    Object.defineProperty(obj, key, {
      get () { return temp },
      set (newval) { 
        self.__$emit(order, newval, temp)
        temp == newval; 
      }
    })
  }

  __$emit (order, newval, oldval) {
    if (this.__event_list[order]) {
      this.__event_list[order].forEach(fn => fn(newval, oldval))
    }
  }

  // $on
  $goWatch (order, fn) {
    this.__event_list[order]
        ? this.__event_list[order].push(fn)
        : this.__event_list[order] = [fn]
  }
}
let obj = {
  a: 1,
  b: {
    name: 'haha'
  }
}
let obj_wc = new Watcher(obj)
obj_wc.$goWatch('a', function (newval, oldval) {
  console.info(`a的值从 ${oldval} 改变为 ${newval} `)
})
obj_wc.$goWatch('a', function (newval, oldval) {
  console.info(`a的值是 ${newval} ,但以前是 ${oldval}`)
})
obj_wc.$goWatch('b.name', function (newval, oldval) {
  console.info(`b.name的值是 ${newval} ,但以前是 ${oldval}`)
})
obj.a = 4
obj.b.name = 'heihei'

相关文章

  • js回调相关知识点(发布订阅)

    Object.prototype.toString.call('str') => "[object String]...

  • JS中的异步操作

    JS中异步编程的方法有: 回调函数 事件监听 发布/订阅 promise generator(ES6) async...

  • 异步操作,promise,generator

    1.js中完成异编程的方法 回调函数 事件监听 发布订阅 Promise对象当多个回调的执行是需要按照一定顺序的时...

  • 异步编程之Promise(一)

    1、Js中常见的异步编程方式? 1)回调函数实现2)发布订阅、通知3)事件监听4)Promise/A+ 和 生成器...

  • JavaScript异步编程简单探索

    关于js异步编程前世今生,从回调函数到事件监听到发布订阅到Promise,网上有很多解释,所以本文仅做对于异步编程...

  • Angular笔记 处理异步

    目前常见的异步编程的几种方法:1、回调函数2、Promise3、Rxjs4、事件监听/发布订阅 一、函数回调 回调...

  • 异步编程解决方案

    事件发布/订阅模式 事件监听器模式是一种广泛用于异步编程的模式,是回调函数的事件化,又称发布/订阅模式。 事件发布...

  • js实现异步编程的4种方法

    1.回调函数2.事件监听3.发布/订阅4.Promise对象

  • 异步

    零. 处理异步常见方法 回调函数事件监听发布/订阅promisegenerator函数async/await 一....

  • 17Generator函数的异步应用

    传统异步 回调函数 响应事件 发布/订阅 Promise Generator 异步案例 适用场景,执行a任务,暂停...

网友评论

      本文标题:js回调相关知识点(发布订阅)

      本文链接:https://www.haomeiwen.com/subject/ymztfctx.html