当前位置:网站首页>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
边栏推荐
- Super easy to use [general excel import function]
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- JS inheritance
- Cefsharp stores cookies and reads cookies
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- Scenario Title: how does system a use the page of system B
- Problem a: face recognition
- 2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
- Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
猜你喜欢

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

Comprehensive calculation of employee information

C interface

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

Why is bi so important to enterprises?

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

Unity basics 2

QT learning summary

超好用的【通用Excel导入功能】

Peut recevoir plusieurs paramètres de type de données - paramètres variables
随机推荐
C set
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
AWS from entry to actual combat: creating accounts
Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
QT dynamic translation of Chinese and English languages
socket编程 send()与 recv()函数详解
[untitled]
Flink real-time data warehouse project - Design and implementation of DWS layer
It can receive multiple data type parameters - variable parameters
Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
场景题:A系统如何使用B系统的页面
Experiment 5 components and event handling
2022年做跨境电商五大技巧小分享
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
批量下載文件----壓縮後再下載
C-10 program error correction (recursive function): number to character
Detailed explanation of socket programming send() and recv() functions
[vs Code] solve the problem that the jupyter file displays exceptions in vs code