当前位置:网站首页>2021-08-11
2021-08-11
2022-04-23 03:26:00 【Open the door of the world】
Vue2 The principle of data response
1、 What is? defineProperty?
defineProperty Is to set object properties , Use the... In the attribute set and get It realizes responsive two-way binding ;
grammar :Object.defineProperty( The object to set , The properties of the object to be modified , Property description )
Property description :
1、configurable - Indicates whether this property can be delete, Default false;
2、enumerable - Indicates whether this property can be enumerated , The default is false;
3、value - Set the value corresponding to this property , The default is undefined;
4、writable - Set up value Whether the property can be modified , by true Can be changed only when , The default is false;
5、get - Provide properties with getter Method , The default is undefined, When accessing this property , The method will be executed , The default parameter is this object ;
6、set - Provide properties with setter Method , The default is undefined, When the attribute value is modified , Will execute the method , The only parameter is the new value ;
Method to get the properties owned by the object :Object.getOwnPropertyDescriptor( object , An attribute of an object )
var obj = {
a: 1,
b: 2
}
var value = obj.b;
Object.defineProperty(obj,‘b’,{
configurable: false,// Can not be deleted
enumerable: false,// Cannot be deleted
writable: true,// The value can be modified
value: ‘22’,// Set up b by 22
set: function(newValue){
console.log(‘ new value by ’+ newValue);
value = newValue;
},
get: function(){
console.log(‘ You set up. value’);
return value;
},
})
console.log(Object.getOwnPropertyDescriptor(obj,‘a’)); //{value: 1, writable: true, enumerable: true, configurable: true}
2、 Implement two-way binding :
function vue(){
this.$data = {a: 1};// Set up data
this.el = document.getElementById(‘app’); // Set the root node
this.dom = ‘’;// fictitious dom
}
// Bind the listening method to the prototype
vue.prototype.observe = function(obj){
var self = this;
var value;
for(var key in obj){
value = obj[key];
if(typeof value === ‘object’){ // If it's an object You need a recursive loop to execute this method
this.observe(value);
}else{
Object.defineProperty(this.$data,key,{
get: function(){
// Rely on collection Skip here Replace the source code depend
return value;
},
set: function(newVal){
value = newVal;
self.render();// Trigger update Replace the source code dep.notify
}
})
}
}
}
vue.prototype.render = function(){
this.dom = ‘ I am a ’ + this.$data.a;
this.el.innerHTML = this.dom;
}
3、 Array listening implementation
vue The properties of arrays : push shift unshift
Object is monitored through defineProperty, And arrays are created by dependArray
Object.create(proto,propertiesObject) // Method creates a new object , Using an existing object to provide a newly created object __proto__
var arrPro = Array.prototype;
var arrObj = Object.create(arrPro);
var arr = [‘push’,‘pop’,‘shift’];
// Decorator mode
arr.forEach(function(method,index){
arrObj[method]=function(){
var ret = arrPro[method].apply(this,arguments);
dep.notify();
return ret;
}
})
Vue3 The corresponding principle of data
let p = new Proxy(target, handler);
target: Target audience
handler: It's an object , Its property is a function that defines the behavior of the agent when an operation is performed .
handler The object methods commonly used in are as follows :
receiver => this Proxy Object itself , That is to say this Point to ;
- get(target, propKey, receiver)
- set(target, propKey, value, receiver)
- has(target, propKey)
- construct(target, args)
- apply(target, object, args)
It is written as follows :
let a = new Proxy({},{
get: function(target,key,receiver){
return Reflect.get(target[key]);
//return target[key];
},
set: function(target, key, value, receiver){
return Reflect.set(target,key,value);
//return target[key] = value;
}
})
Object.defineProperty There are the following disadvantages :
- Methods listening to arrays cannot trigger Object.defineProperty Methods set operation ( If you want to listen , You need to rewrite the array method ).
- You have to traverse every attribute of every object , If objects are deeply nested , You need to use recursive calls .
Proxy comparison defineProperty difference :
1、 The parameters are different ,defineProperty The second parameter needs to specify the specific of the operation object key value , and Proxy Specifies the outermost object , This eliminates the cycle ;
2、defineProperty The object itself is manipulated , Changed the object itself , and Proxy Is to change the object proxy , Return to new object ;
3、defineProperty You must define a global variable at the outermost layer value, adopt get Put it return get out , and Proxy Put the return value into the parameter , direct return target[key];
Diff Algorithm and virtual dom
notes :virtual dom It's a virtual layer , It doesn't officially exist
diff The algorithm is to compare elements directly , The element contains props and children, Go all the way to the root node ,template All nodes in the will have a diff, Mount a series of bound properties , Listen for changes by traversing the loop , stay vue3 The author changed this way , In the press conference, we talked about the comparison vue2 Speed up 6 times ,
vue2 The virtual dom, Will traverse all child nodes , All use diff The algorithm is compared once , Traversal speed is determined by dom Quantity determination ;
vue3 Will find the items that change , Update only changes ;
版权声明
本文为[Open the door of the world]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220619451711.html
边栏推荐
- js递归树结构计算每个节点的叶子节点的数量并且输出
- C abstract class
- 2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
- 关于idea调试模式下启动特别慢的优化
- "Visual programming" test paper
- 可以接收多種數據類型參數——可變參數
- Web Course Design - his system
- 集合之List接口
- Huawei mobile ADB devices connection device is empty
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
猜你喜欢

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案

New ORM framework -- Introduction to beetlsql

Configuration table and page information automatically generate curd operation page

Supersocket is Use in net5 - concept

File upload vulnerability summary and upload labs shooting range documentary

一文了解全面静态代码分析

Fiddler use
![[untitled]](/img/b5/6ce72422bbf330610c747ceb482944.jpg)
[untitled]

Unity basics 2

AWS from entry to actual combat: creating accounts
随机推荐
poi根据数据创建导出excel
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
你真的懂hashCode和equals吗???
Queue storage and circular queue
ThreadLocal 测试多线程变量实例
2022年做跨境电商五大技巧小分享
全新的ORM框架——BeetlSQL介绍
Idea view history [file history and project history]
2022g2 boiler stoker examination question bank and online simulation examination
L3-011 直捣黄龙 (30 分)
QT dynamic translation of Chinese and English languages
ThreadLocal test multithreaded variable instance
Unity basics 2
Explanation keyword of MySQL
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Problem C: Hanoi Tower III
Explication détaillée des fonctions send () et recv () du programme Socket
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
Visual programming - drawing assignment
TCP three handshakes and four waves