申明变量对象

Object.defineProperty
var obj = {'name':'1111','id':'001'},news = null;
Object.defineProperty(obj,'obj',{
    get:function(){
        console.log('get')
        return news
    },
    set:function(val){
        console.log('来取值')
        news = val
    }
})
// 改变name值
obj.name = 121211
console.log(obj.name) // 输出新的name

Proxy
var news = new Proxy(obj,{
    get(target,key){
        console.log(key,target,'get')
        return target[key]
    }
})
obj.name = 121211
console.log(obj) // 输出 {name: 121211, id: 001}

码厨
27 声望5 粉丝

学无止境...