xinggevip

Es2015 proxy之get
index.html<!DOCTYPE html> <html lang="en&qu...
扫描右侧二维码阅读全文
20
2019/06

Es2015 proxy之get

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>

    <script>
        /*
            proxy 代理

        */
        let obj = {
            name: 'tom',
            age:18
        };
        let newObj = new Proxy(obj,{
            get(target,property){
                console.log(target);    // {name: "tom", age: 18}
                console.log(property);  // age

                if(property in target){
                    return target[property];    // 不能直接点出来,只能加中括号
                }else{
                    // 找不到属性则抛出异常
                    throw new ReferenceError(`对象找到不到属性${property}`);
                }

                
            }
        });
        console.log(newObj.age);

    </script>
</body>

</html>
Last modification:June 20th, 2019 at 09:45 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment