vue is how to achieve a responsive

 Today looked at the vue in the end is how to achieve responsive, and found to be particularly simple, is the use of Object.defineProperty this function to listen,

This is a demo

            var vm = {};
            var data = {
                name: 'zhangsan',
                age: 18
            }
            var key , value;
            for ( key in data) {
                (function(key) {
                    
                    Object.defineProperty(vm,key,{
                        get: function() {
                            console.log('get');
                            return data[key];
                        },
                        set: function(newValue){
                            console.log('set');
                            data[key] = newValue;
                        }
                    })
                    
                })(key)
            }

 

The first parameter corresponds to a vue example, the second parameter is a data key, the third parameter is an object, the object contains get and set the two functions, are listening to retrieve and modify

In the console access and modify the value will be printed out and get set

 

 This realization interception data can be achieved in response to a formula

Guess you like

Origin www.cnblogs.com/lilei-site/p/11622192.html