• 代理


    {
        //创建一个供应商的属性
        let obj ={
            time:'2017-03-11',
            name:'net',
            _r:123
        }
        let monitor = new Proxy(obj,{
            //拦截对象属性的读取
            get(target,key){
                return target[key].replace('2017','2020')
            },
            set(target,key,val){
                if(key === 'name'){
                    return target[key] = val
                }else{
                    return target[key]
                }
            },
            has(target,key){
                if(key === name){
                    return target[key]
                }else{
                    return false
                }
            },
            deleteProperty(target,key){
                if(key.indexOf('_')> -1){
                    delete  target[key]
                    return true
                }else{
                    return target[key]
                }
            }
        })
        //读取属性
        //console.log('get',monitor.time)
        //console.log('set',monitor.time,'2019')
        monitor.time = 2019
        monitor.name = 'java'
        // console.log(monitor.name)
        // console.log('has','name' in monitor, 'time' in monitor)
        // console.log(monitor.time)
         delete monitor.time
        console.log(monitor)
        delete monitor._r
        console.log(monitor)
    }
    

      

  • 相关阅读:
    svg
    vuex的模块
    es6的新增方法和es5数组的一些方法
    vue的响应规则
    简单的解构赋值
    vuex的四种状态
    indexDB
    token验证
    C# 委托与事件的DEMO
    WPF MVVM 键盘按键事件绑定
  • 原文地址:https://www.cnblogs.com/joer717/p/11457765.html
Copyright © 2020-2023  润新知