baidu-动态数据绑定(1)

 
  
     function Event() {
            this.events = {};
        }

        Event.prototype.on = function(attr, callback) {
            if(this.events[attr]) {
                this.events[attr].push(callback);
            } else {
                this.events[attr] = [callback];
            }
        }

        Event.prototype.off = function(attr) {
            for(let key in this.events) {
                if(this.events.hasOwnProperty(key) && key === attr) {
                    delete this.events[key];
                }
            }
        }

        Event.prototype.emit = function(attr, ...arg) {
            this.events[attr] && this.events[attr].forEach(function(item) {
                item(...arg);
            })
        }
            // 观察者构造函数
        function Observer(data) {
            //暂不考虑数组
            this.data = data;
            this.makeObserver(data);
            this.eventsBus = new Event();
        }

        Observer.prototype.setterAndGetter = function(key, val) {
            let _this = this;
            Object.defineProperty(this.data, key, {
                enumerable: true,
                configurable: true,
                get: function() {
                    console.log('你访问了' + key);
                    return val;
                },
                set: function(newVal) {
                    console.log('你设置了' + key);
                    console.log('新的' + key + '=' + newVal);
                    //触发$watch函数
                    _this.eventsBus.emit(key, val, newVal);
                    val = newVal;
                    //如果newval是对象的话
                    if(typeof newVal === 'object') {
                        new Observer(val);
                    }
                }
            })
        }

        Observer.prototype.makeObserver = function(obj) {
            let val;
            for(let key in obj) {
                if(obj.hasOwnProperty(key)) {
                    val = obj[key];
                    //深度遍历
                    if(typeof val === 'object') {
                        new Observer(val);
                    }
                }
                this.setterAndGetter(key, val);
            }
        }

        Observer.prototype.$watch = function(attr, callback) {
            this.eventsBus.on(attr, callback);
        }

        let app = new Observer({
            name: 'liujianhuan',
            age: 25,
            company: 'Qihoo 360',
            address: 'Chaoyang, Beijing'
        })

        app.$watch('age', function(oldVal, newVal) {
            console.log(`我的年龄变了,原来是: ${oldVal}岁,现在是:${newVal}岁了`)
        })

        app.$watch('company', function(oldVal, newVal) {
            console.log(`啊啊啊啊啊`)
        })
 
  

 

 

 

转载于:https://www.cnblogs.com/xianyuyuyu/p/6524301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值