1. Advantages of proxy vs. Object.definePropertyAdvantages of proxy:
Advantages of Object.defineProperty: Good compatibility: Supports IE9, but Proxy has browser compatibility issues, and it cannot be smoothed out with polyfill, so the author of Vue stated that it is necessary to wait until the next major version (3.0) to rewrite it with Proxy. 2. Simple implementation of proxy monitoring object1. Simple implementation of proxy object```javascript let data = {}; // Define an empty object let proxy = new Proxy(data, {}); // Create a Proxy and use data as the target object // Modify the name attribute of the Proxy proxy object proxy.name = 'shelley'; console.log(proxy); console.log(data) // { name: 'shelley' } // { name: 'shelley' } ``` 2. Supplementary knowledge Reflect We need to return a Reflect.set(...
3. Proxy methodThe handler.set() method attributes set the catcher for the operation. ```javascript let data = { name: 'shelley', age: '27' }; let p = new Proxy(data, { set(target, prop, value) { // target = target object // prop = set property // value = modified value console.log(target, prop, value); // { name: 'shelley', age: '27' } age 18 return Reflect.set(...arguments); } }) p.age = 18; console.log(data); // { name: 'shelley', age: 18 } ``` - handler.get() Capture of attribute read operations. ```javascript let data = { name: 'shelley', age: 22 }; let p = new Proxy(data, { get(target, prop){ console.log(target, prop); //{ name: 'shelley', age: 22 } age return Reflect.get(...arguments); } }) console.log(p.age);//22 ``` Simple implementation of Object.defineProperty monitoring object ```javascript var o = {}; // Create a new object var bValue = 39; // Add an instance of the accessor descriptor property to the object Object.defineProperty(o, 'bValue', { // This code will not set the properties of o, it will only get() when accessed { return bValue; }, set(newValue) { console.log('set==>', newValue); bValue = newValue; } }); console.log(o) // {} // Enter the get method of the bValue property of the accessor proxy, return, and set the value of bValue in the o object to 38 console.log(o.bValue); // 38 // Enter the set method of the bValue property of the accessor proxy and set the new value of bValue. // Enter get again and return, and set the value of bValue in the o object to 40 o.bValue = 40; console.log(o.bValue) // 40 ``` summary:
3. Handwritten vue3.0 two-way binding-es6 Proxy1. What is Proxy
2. Use two-way binding in vue.js```javascript <div id="app"> <h2>{{msg}}</h2> <input type="text" v-model="msg"/> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> let vm = new Vue({ el: '#app', data: { msg: 'shelley' }, }) </script> ``` Proxy vs. Object.definePropertyTwo-way binding in Vue 2.0, using Object.defineProperty() for two-way binding shortcoming:
- Object.definePorperty() recursively traverses all properties of all objects, which will affect performance when the data hierarchy is deep. - Object.definePorperty() can only be used on objects, not on arrays. - Object.definePorperty() can only monitor the properties during definition, not newly added properties. - Since Object.definePorperty() cannot be used on arrays, vue2.0 chooses to monitor array data by rewriting the array method prototype, but it still cannot monitor changes in array indexes and lengths. Two-way binding in Vue 3.0, using Proxy and Reflect for two-way binding advantage:
shortcoming:
Solution:
Proxy can only proxy one layer and cannot monitor deeply
This is the end of this article about the principle of proxy implementation of Vue3 data two-way binding. For more relevant content about proxy implementation of Vue3 data two-way binding, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Complete steps to install Anaconda3 in Ubuntu environment
>>: Conditional comments to determine the browser (IE series)
In order to centrally manage the images we create...
Preface: position:sticky is a new attribute of CS...
“Inputs should be divided into logical groups so ...
There are two common loading icons on the web, on...
Preface In today's increasingly convenient In...
There are 4 commonly used methods, as follows: 1....
Table of contents 1. Preparation 2. MySQL encrypt...
1. scroll-view When using vertical scrolling, you...
Step 1: Get the MySQL YUM source Go to the MySQL ...
1. The ENV instruction in the Dockerfile is used ...
1. Display effect: 2, html structure <div clas...
environment: 1 CentOS Linux release 7.5.1804 (Cor...
1. Problem description <br />When JS is use...
Managing disk space is an important daily task fo...
It is very simple to build a go environment under...