MCPcopy
hub / github.com/ygs-code/vue / defineReactive

Function defineReactive

vue.js:1483–1559  ·  view source on GitHub ↗

* Define a reactive property on an Object. * 在对象上定义一个无功属性。 * 更新数据 * 通过defineProperty的set方法去通知notify()订阅者subscribers有新的值修改 * 添加观察者 get set方法

(obj, //对象
        key,//对象的key
        val, //监听的数据 返回的数据
        customSetter, //  日志函数
        shallow //是否要添加__ob__ 属性
    )

Source from the content-addressed store, hash-verified

1481 * 添加观察者 get set方法
1482 */
1483 function defineReactive(obj, //对象
1484 key,//对象的key
1485 val, //监听的数据 返回的数据
1486 customSetter, // 日志函数
1487 shallow //是否要添加__ob__ 属性
1488 ) {
1489 //实例化一个主题对象,对象中有空的观察者列表
1490 var dep = new Dep();
1491 //获取描述属性
1492 var property = Object.getOwnPropertyDescriptor(obj, key);
1493 var _property = Object.getOwnPropertyNames(obj); //获取实力对象属性或者方法,包括定义的描述属性
1494 console.log(property);
1495 console.log(_property);
1496
1497 if (property && property.configurable === false) {
1498 return
1499 }
1500
1501 // cater for pre-defined getter/setters
1502
1503 var getter = property && property.get;
1504 console.log('arguments.length=' + arguments.length)
1505
1506 if (!getter && arguments.length === 2) {
1507 val = obj[key];
1508 }
1509 var setter = property && property.set;
1510 console.log(val)
1511 //判断value 是否有__ob__ 实例化 dep对象,获取dep对象 为 value添加__ob__ 属性递归把val添加到观察者中 返回 new Observer 实例化的对象
1512 var childOb = !shallow && observe(val);
1513 //定义描述
1514 Object.defineProperty(obj, key, {
1515 enumerable: true,
1516 configurable: true,
1517 get: function reactiveGetter() {
1518
1519 var value = getter ? getter.call(obj) : val;
1520 if (Dep.target) { //Dep.target 静态标志 标志了Dep添加了Watcher 实例化的对象
1521 //添加一个dep
1522 dep.depend();
1523 if (childOb) { //如果子节点存在也添加一个dep
1524 childOb.dep.depend();
1525 if (Array.isArray(value)) { //判断是否是数组 如果是数组
1526 dependArray(value); //则数组也添加dep
1527 }
1528 }
1529 }
1530 return value
1531 },
1532 set: function reactiveSetter(newVal) {
1533 var value = getter ? getter.call(obj) : val;
1534 /* eslint-disable no-self-compare 新旧值比较 如果是一样则不执行了*/
1535 if (newVal === value || (newVal !== newVal && value !== value)) {
1536 return
1537 }
1538 /* eslint-enable no-self-compare
1539 * 不是生产环境的情况下
1540 * */

Callers 5

vue.jsFile · 0.85
setFunction · 0.85
loopFunction · 0.85
initInjectionsFunction · 0.85
initRenderFunction · 0.85

Calls 2

observeFunction · 0.85
dependArrayFunction · 0.85

Tested by

no test coverage detected