学习vue 2.x源码笔记

1、响应式原理:

核心:Object.defineProperty,用法如下:

var obj1 = {};
var initValue = 'hello';
Object.defineProperty(obj1,"newKey1",{
       //writable:true,
    //value:123,
    //enumerable:true,
    configurable:true,
    get:function(){
        return initValue;
    },
    set:function(val){
        console.log('set:',val);
        initValue = val;
    }
});

obj1.newKey1; //->"hello"
obj1.newKey1 = 'modified';//->set: modified    
                                        //->"modified"

 

posted @ 2019-04-23 16:32  imsomnus  阅读(159)  评论(0编辑  收藏  举报