1

Reflect(反射)

Reflect Proxy 一样都是 es6 用来操作对象的API(接口)

var obj={
    name:'shi',
    age:19,
    sex:'女'
}
//obj.school="师徒"
//Object.defineProperty(obj,"school",{value:"师徒"});
Reflect.defineProperty(obj,"school",{value:"师徒"});
console.log(obj);//Object {name: "shi", age: 19, sex: "女", school: "师徒"}

2.让object的操作都变成函数行为


//delete obj.age
console.log(obj);//Object {name: "shi", sex: "女", school: "师徒"}
Reflect.deleteProperty(obj,'age');

console.log('sex' in obj);//true
console.log(Reflect.has(obj,'sex'));//true

Proxy(代理。拦截器)

直接操作
    var obj={
    name:'shi',
    age:19,
    sex:'女'
}
obj.name="shishi";
console.log(obj.age);//19
console.log(obj);//Object {name: "shishi", age: 19, sex: "女"}*/
代理操作
var obj={
    name:'shi',
    age:19,
    sex:'女'
}
var proxObj=new Proxy(obj,{
    //赋值
    sex:function(target,key,value){
        if(value!="shi1shi"){
            target[key]=value;
            
        }
    },
    //取值
    get:function(targer,key){
        if(targer[key]==undefined){
            return "没有该属性"
        }
    }
});
proxObj.name="shishi";
console.log(proxObj);//{name: "shishi", age: 19, sex: "女"}
console.log(proxObj.legs);//没有该属性

师妹儿
195 声望9 粉丝

html/css/html5/javascript/jquery/es6/svg