1、由object.defineProperty()向Proxy()的转变
使用object.defineProperty()写一个简单的双向绑定:
Vue.prototype.observe = function(obj){
var self = this;
var value;
for(var key in obj){
value = obj[key];
if(typeof value === 'object'){
this.observe(value);//判断值为对象时,递归
}else{
object.defineProperty(this.$data,key,{
get:function(){
return value;////需要借助外部value return值
},
set:function(newValue){
value = newValue;
//render() 更新视图函数
}
})
}
}
}
使用Proxy()写一个简单的双向绑定:
Vue.property.observe = function(obj){
var self = this;
this.$data = new Proxy(this.$data,{
get:function(){};
set:function(){}
})
}
使用Proxy()的优点:
- 通过代码量可以看出通过Proxy()写出来的代码量更少
- Proxy()不需要具体传对象的属性,省去了for…in循环。
- object.defineProperty()改变对象的属性标签
Proxy()不会去改变原对象,而是根据原对象重新包装一个代理对象,若要触发get,set,要通过代理对象来操作
2、重新定义vdom对比思路
vue2速度由html规模大小决定
vue3通过block-tree思想通过给动态属性或标签添加标记来拆分vdom
3、编程方式改为函数式
为什么要用函数式:
1、方便组合逻辑
2、tree-shaking打包时将没有用到的方法删除掉
3、更好压缩(压缩函数名)
4、其他改动
typeScript的支持(vue3源码很多是用typeScript写的)