1.vue2双向数据绑定实现
vue2主要借助于object.defineProperty实现(讲解)

数据描述符:
1.configurable(默认false)值为true才能在该属性对应的对象上被删除




2.enumerbale(默认值:false) 为true此属性可枚举




3.value:(默认值undefined)表示给该属性对应的值,可以是函数、对象、数值等。


4. writable(默认值是false) 当值为true的时候,此属性才会被重新赋值。


存取描述符:
1.get(默认值undefined):当访问此属性就会调用该函数,执行时不需要传入任何参数,但会传入this对象(由于继承关系 this不一定是定义该属性的对象),该函数的返回值会被用作与属性的值。




2.set(默认值:undefined):当属性值被修改会调用此函数,该方法接受一个参数(赋予的新值),会传入赋值时的this对象。


Object.defineProperty缺点:
1.无法检测到对象属性的新增或删除
2.由于js的动态性,可以对对象追加新的属性或者删除其中的属性.这点对经过Object.defineProperty方法建立的响应式对象来说,只能追踪对象已有数据是否被修改,无法追踪新增属性和删除属性,需另外处理.
网友评论