handler和proxy的用法

const monster1 = {
    
    
    eyeCount: 4
};

const monsterPrototype = {
    
    
    eyeCount: 2
};

const handler = {
    
    
    getPrototypeOf(target) {
    
    
        return monsterPrototype;
    }
};

const proxy1 = new Proxy(monster1, handler);

console.log(Object.getPrototypeOf(proxy1) === monsterPrototype);
console.log(Object.getPrototypeOf(proxy1))
console.log(monsterPrototype)
console.log(monster1)

console.log(Object.getPrototypeOf(proxy1).eyeCount)

语法

const p = new Proxy(obj, {
    
    
  getPrototypeOf(target) {
    
    
  ...
  }
});

参数
当 getPrototypeOf 方法被调用时,this 指向的是它所属的处理器对象。

target
被代理的目标对象。
返回值
getPrototypeOf 方法的返回值必须是一个对象或者 null。

猜你喜欢

转载自blog.csdn.net/weixin_40945354/article/details/120127155