码迷,mamicode.com
首页 > 其他好文 > 详细

es6 Reflect 与 Proxy

时间:2020-07-26 15:07:45      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:let   type   ESS   return   get   code   es6   typeerror   target   

<script type="text/javascript">

    let target = {
        name:"小明",
        age: 15
    }

    let handler = {
        get(target, propKey, receive){
            if(propKey in target){
                console.log("success");
            }else{
                console.log("error")
            }
            return Reflect.get(target, propKey, receive);
        },
        set(target, propKey, value, receiver){
            if(propKey==‘age‘){
                if(!Number.isInteger(value)){
                    throw new TypeError(‘The age is not an integer‘);
                }else{
                    console.log("set success");
                }
            }else{
                console.log("set success");
            }
            return Reflect.set(target, propKey, value, receiver);
        },
        has(target,proKey){
            console.log(‘handle has‘);
            return proKey in target;
        }
    }

    let pro = new Proxy(target,handler);
    console.log(‘name‘ in pro)
    console.log(‘sex‘ in pro)
    /*pro.name=‘李四‘
    pro.age=34
    console.log(pro.name)
    console.log(pro.age)*/
</script>

 

es6 Reflect 与 Proxy

标签:let   type   ESS   return   get   code   es6   typeerror   target   

原文地址:https://www.cnblogs.com/Essaycode/p/13379457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!