1. 发布者-订阅者模式,如backbone.js
  2. 脏值检查,如angular,对常用的dom及xhr事件做了封装,在其中会从rootscope开始进行遍历,检查所有的watcher,并不是定时轮询检查
  3. Object.defineProperty,此属性可以设置对象的get和set,在对对象进行读写的时候就会触发相应的函数。示例代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    let boundObj = { a: 1 }
    let resObj = {}
    boundObj.bValue = boundObj.a
    Object.defineProperty(boundObj, a, {
    get(){
    return boundObj.bValue
    },
    set(newValue){
    boundObj.bValue = newValue
    resObj.b = newValue
    return newValue
    }
    })

    resObj对象的b属性便与boundObj的a属性绑定了

  4. proxy,通过设置set来进行绑定,相对于Object.defineProperty来说更加简洁,不再需要中间变量
    1
    2
    3
    4
    5
    6
    7
    8
    9
    const boundObj = { a: 1 }
    let resObj = {}
    boundObj.proxy = new Proxy(boundObj,{
    set(target, prop, value, receiver){
    target[prop] = value
    resObj[prop] = value
    return true
    }
    })