zoukankan      html  css  js  c++  java
  • 【JavaScript】Proxy 实例方法(二)

    以下内容为学习记录,可以参考 MDN 原文。

    环境

    • node v12.18.1
    • npm 6.14.5
    • vscode 1.46
    • Microsoft Edge 83

    概念

    Proxy 对象用于定义基本操作的自定义行为(如属性查找、赋值、枚举、函数调用等)。

    has

    handler.has() 方法是针对 in 操作符的代理方法。

    const handler1 = {
      has(target, key) {
        if (key[0] === '_') {
          return false;
        }
        return key in target;
      }
    };
    
    const monster1 = {
      _secret: 'easily scared',
      eyeCount: 4
    };
    
    const proxy1 = new Proxy(monster1, handler1);
    console.log('eyeCount' in proxy1);
    // expected output: true
    
    console.log('_secret' in proxy1);
    // expected output: false
    
    console.log('_secret' in monster1);
    // expected output: true
    

    isExtensible

    handler.isExtensible() 方法用于拦截对对象的 Object.isExtensible()。

    const monster1 = {
      canEvolve: true
    };
    
    const handler1 = {
      isExtensible(target) {
        return Reflect.isExtensible(target);
      },
      preventExtensions(target) {
        target.canEvolve = false;
        return Reflect.preventExtensions(target);
      }
    };
    
    const proxy1 = new Proxy(monster1, handler1);
    
    console.log(Object.isExtensible(proxy1));
    // expected output: true
    
    console.log(monster1.canEvolve);
    // expected output: true
    
    Object.preventExtensions(proxy1);
    
    console.log(Object.isExtensible(proxy1));
    // expected output: false
    
    console.log(monster1.canEvolve);
    // expected output: false
    

    ownKeys

    handler.ownKeys() 方法用于拦截 Reflect.ownKeys().

    const monster1 = {
      _age: 111,
      [Symbol('secret')]: 'I am scared!',
      eyeCount: 4
    };
    
    const handler1 = {
      ownKeys(target) {
        return Reflect.ownKeys(target);
      }
    };
    
    const proxy1 = new Proxy(monster1, handler1);
    
    for (let key of Object.keys(proxy1)) {
      console.log(key);
      // expected output: "_age"
      // expected output: "eyeCount"
    }
    

    preventExtensions

    handler.preventExtensions() 方法用于设置对 Object.preventExtensions() 的拦截。

    const monster1 = {
      canEvolve: true
    };
    
    const handler1 = {
      preventExtensions(target) {
        target.canEvolve = false;
        Object.preventExtensions(target);
        return true;
      }
    };
    
    const proxy1 = new Proxy(monster1, handler1);
    
    console.log(monster1.canEvolve);
    // expected output: true
    
    Object.preventExtensions(proxy1);
    
    console.log(monster1.canEvolve);
    // expected output: false
    

    set

    handler.set() 方法是设置属性值操作的捕获器。

    function Monster() {
      this.eyeCount = 4;
    }
    
    const handler1 = {
      set(obj, prop, value) {
        if ((prop === 'eyeCount') && ((value % 2) !== 0)) {
          console.log('Monsters must have an even number of eyes');
        } else {
          return Reflect.set(...arguments);
        }
      }
    };
    
    const monster1 = new Monster();
    const proxy1 = new Proxy(monster1, handler1);
    proxy1.eyeCount = 1;
    // expected output: "Monsters must have an even number of eyes"
    
    console.log(proxy1.eyeCount);
    // expected output: 4
    

    setPrototypeOf

    handler.setPrototypeOf() 方法主要用来拦截 Object.setPrototypeOf()。

    const handler1 = {
      setPrototypeOf(monster1, monsterProto) {
        monster1.geneticallyModified = true;
        return false;
      }
    };
    
    const monsterProto = {};
    const monster1 = {
      geneticallyModified: false
    };
    
    const proxy1 = new Proxy(monster1, handler1);
    // Object.setPrototypeOf(proxy1, monsterProto); // throws a TypeError
    
    console.log(Reflect.setPrototypeOf(proxy1, monsterProto));
    // expected output: false
    
    console.log(monster1.geneticallyModified);
    // expected output: true
    
  • 相关阅读:
    xampp只允许本地访问,禁止远程访问
    Centos 7 安装 设置 IP地址,DNS,主机名,防火墙,端口,SELinux (实测+笔记)
    centos 7.0 网卡配置及重命名教程
    VS2010 安装 MVC3 Entity Framework
    SQL 增加删除库表
    轻松绕各种WAF的POST注入、跨站防御(比如安全狗)
    CRM协同8.2升级到9.2SP2步骤
    [转]EasyUI+MVC+EF简单用户管理Demo(问题及解决)
    设置VMware随系统开机自动启动并引导虚拟机操作系统
    项目管理——随笔 2015.06.05
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13688526.html
Copyright © 2011-2022 走看看