vue2中使用到了Object.defineProperty这个属性,虽然3.0刚刚发布,3.0用的是proxy进行数据解决Object.defineProperty的无法监听 属性的添加和删除、数组索引和长度的变更等缺陷,这里仅仅记录2中的原理

Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="name"></div>
  <script>
  var data= {}
  Object.defineProperty(data,'name',{
    get:function(){
      return document.querySelector('#name').innerHTML
    },
    set:function(val){
      return document.querySelector('#name').innerHTML = val
    }
  })
  data.name = 'wumao'
  </script>
</body>
</html>

所以现在山寨一个Vue,实现最简单的数据劫持

class Wumao {
  constructor(options) {
    //缓存data
    this.$data = options.data
    //调用响应式函数
    this.observer(this.$data)
  }
  observer(obj) {
    if (!obj || typeof obj !== 'object') {
      return
    }
    //先遍历出data的key出来然后遍历他去使用definePorperty去监听
    Object.keys(obj).forEach(key => {
      this.defineReactive(obj, key, obj[key])
    })
  }
  defineReactive(obj, key, val) {
    this.observer(val) //递归进行深度监听
    Object.defineProperty(obj, key, {
      enumerable: true /* 属性可枚举 */ ,
      configurable: true /* 属性可被修改或删除 */ ,
      get() {
        return val;
      },
      set(newVal) {
        if (newVal === val) return;
        console.log("更新数据了", val);
      }
    });
  }
}
Last modification:October 9, 2019
If you think my article is useful to you, please feel free to appreciate