重新整理vue2响应式

上一篇文章:vue2响应式

    //Vue
    class Vue {
        constructor(options) {
            this.$el = options.el
            this._data = options.data
            this.$data = this._data

            new Observer(this.$data)
        }
    }
    //观察者
    class Observer {
        constructor(object) {
            if (typeof object === "object") {
                this.walk(object)
            }
        }
        walk(object) {
            Object.keys(object).forEach(key => {
                this.defineReactive(object, key)
            })
        }
        defineReactive(object, key) {
            if (typeof object[key] === "object") {
                this.walk(object[key])
            }
            let dep = new Dep()
            let val = object[key]
            Object.defineProperty(object, key, {
                enumerable: true,
                configurable: true,
                get() {
                    Dep.target && dep.addSub(Dep.target)
                    return val
                },
                set(newValue) {
                    val = newValue
                    dep.notify(newValue)
                }
            })

        }

    }

    //Dep
    class Dep {
        static target = null
        constructor() {
            this.subs = []
        }
        addSub(watcher) {
            this.subs.push(watcher)
        }
        notify(newValue) {
            this.subs.forEach(sub => {
                sub.update(newValue)
            })
        }
    }

    let uid = 0
    //watcher
    class Watcher {
        constructor(vm, key, cb) {
            this.vm = vm
            this.uid = uid++
            this.cb = cb
            Dep.target = this
            this.value = findKey(this.vm.$data, key)
            Dep.target = null
        }
        update(newValue) {
            if (this.value !== newValue) {
                this.value = newValue;
                this.run();
            }
        }
        run() {
            this.cb(this.value)
        }
    }


    let data = {
        name: "花生",
        age: 17,
        job: {
            salary: 22
        }
    }
    let vm = new Vue({ data })
    watch(data)

    function findKey(object, target) {
        if (typeof object !== "object") {
            return;
        }
        if (Object.keys(object).includes(target)) {
            return object[target]
        } else {
            Object.keys(object).forEach(key => {
                if (typeof object === "object") {
                    findKey(object[key], target)
                }
            })
        }
    }
    function watch(object) {
        if (typeof object !== "object") {
            return;
        }
        Object.keys(object).forEach(key => {
            if (typeof object[key] === "object") {
                watch(object[key])
            } else {
                new Watcher(vm, key, (value) => {
                    console.log("watcher", value);
                })
            }

        })
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值