Before talking about data responsiveness, we need to solve a very important problem, which is what exactly does Vue do with data? Let's start with getters and setters, which we use to read and write virtual properties. Getters and settersThere is the following code let obj0 = { Last name: "高", Name: "Yuan Yuan", age: 18 }; // Requirement 1, get the name let obj1 = { Last name: "高", Name: "Yuan Yuan", Name() { return this.surname + this.name; }, age: 18 }; console.log("Requirement 1: " + obj1.Name()); //Gao Yuanyuan At this time, the result we logged out is Gao Yuanyuan, which everyone can understand, but can the brackets after the name be deleted? No, because it is a function, so how do we remove the parentheses? Here are our requirements // Requirement 2: The name can be obtained without brackets let obj2 = { Last name: "高", Name: "Yuan Yuan", getName() { return this.surname + this.name; }, age: 18 }; console.log("Requirement 2: " + obj2.Name); //Gao Yuanyuan At this time we use getter, and we can get the value without brackets. So how do we change the name? // Requirement 3: The name can be written let obj3 = { Last name: "高", Name: "Yuan Yuan", getName() { return this.surname + this.name; }, set name(xxx){ this.surname = xxx[0] this.name = xxx.slice(1) }, age: 18 }; obj3.Name = 'Gao Yuanyuan' console.log(`Requirement 3: Last name ${obj3.last name}, First name ${obj3.first name}`) // Gao Yuanyuan Where there is get, there is set, and that's how setter is used. We use the attribute value = xxx to trigger the set function, and the name can be written. However, if we type Why is Object.defineProperty In the above example, we use get and set directly when defining the object, but if the object has been declared, how do we continue to add get? We need to use var _xxx = 0 Object.defineProperty(obj3,'xxx',{ get(){ return _xxx }, set(value){ _xxx= value } }) Next we can solve the initial problem: what exactly does Vue do with data? Let’s take a look at a few examples: let data0 = { n: 0 } First declare a data0, requirement 1: define n using let data1 = {} Object.defineProperty(data1, 'n', { value: 0 }) console.log(`Requirement 1: ${data1.n}`) //Requirement 1: 0 Requirement 2: n cannot be less than 0: let data2 = {} data2._n = 0 // _n is used to secretly store the value of n, the default value is 0 Object.defineProperty(data2, 'n', { get(){ return this._n }, set(value){ if(value < 0) return this._n = value } }) console.log(`Requirement 2: ${data2.n}`)//0 data2.n = -1 console.log(`Requirement 2: ${data2.n} is set to -1 and fails`) //0 is set to -1 and fails data2.n = 1 console.log(`Requirement 2: ${data2.n} is set to 1 Success`) //0 is set to 1 Success But what if the other party uses let data3 = proxy({ data:{n:0} }) // The parentheses are anonymous objects and cannot be accessed function proxy({data}){ const obj = {} // The 'n' here is hardcoded. In theory, it should traverse all the keys of data. I have simplified it here // because I am afraid you can't understand Object.defineProperty(obj, 'n', { get(){ return data.n }, set(value){ if(value<0)return data.n = value } }) return obj // obj is the proxy} // data3 is obj console.log(`Requirement 3: ${data3.n}`) data3.n = -1 console.log(`Requirement 3: ${data3.n}, set to -1 failed`) data3.n = 1 console.log(`Requirement 3: ${data3.n}, set to 1 success`) But what if you don’t want to use an agent? let myData = {n:0} let data4 = proxy({ data:myData }) // The object in brackets is anonymous and cannot be accessed // data3 is obj console.log(`Argumentative: ${data4.n}`) //0 myData.n = -1 console.log(`Argumentative: ${data4.n}, set to -1 failed!?`) Now myData can still be modified in this way, so we have another requirement: even if the user modifies myData without authorization, we must intercept it: let myData5 = {n:0} let data5 = proxy2({ data:myData5 }) // The parentheses are anonymous objects and cannot be accessed function proxy2({data}){ // The 'n' here is hardcoded. In theory, we should traverse all the keys of data, but we simplify it here. let value = data.n //Save the starting n Object.defineProperty(data, 'n', {//Declare a new n get(){ return value }, set(newValue){ if(newValue<0)return value = newValue } }) Just add the above sentences, these sentences will monitor data const obj = {} Object.defineProperty(obj, 'n', { get(){ return data.n }, set(value){ if(value<0)return//This sentence is redundant data.n = value } }) return obj // obj is the proxy} // data3 is obj console.log(`Requirement 5: ${data5.n}`) //0 myData5.n = -1 console.log(`Requirement 5: ${data5.n}, set to -1 failed`) //0 myData5.n = 1 console.log(`Requirement 5: ${data5.n}, set to 1 successfully`) //1 When we write
Then we can go back to the title, what is data responsiveness? An object is responsive if it can react to external stimuli. Vue's data is responsive. In The above is the detailed summary of Vue data responsiveness. For more information about Vue data responsiveness, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of using tcpdump command to capture and analyze data packets in Linux
>>: MySQL learning notes: data engine
Sometimes we build a file server through nginx, w...
Table of contents Axios Request Qs processing dat...
Nginx supports three ways to configure virtual ho...
MySql 8.0 corresponding driver package matching A...
Table of contents Why use a debugger? Basic usage...
Case 1 vue-cli builds the vue3 project, uploads t...
This article records the detailed installation tu...
【Historical Background】 I have been working as a ...
Table of contents Method 1: Call the function dir...
In MySQL 8.0.18, a new Hash Join function was add...
Drop-down box, text field, file field The upper p...
I have written such an article before, but I used...
Install MySQL for the first time on your machine....
Deleting a table is not very common, especially f...
1. Refer to the official website to install docke...