zoukankan      html  css  js  c++  java
  • Proxies

    Proxies

    Proxies enable creation of objects with the full range of behaviors available to host objects. Can be used for interception, object virtualization, logging/profiling, etc.

    // Proxying a normal object
    var target = {};
    var handler = {
      get: function (receiver, name) {
        return `Hello, ${name}!`;
      }
    };
    
    var p = new Proxy(target, handler);
    p.world === "Hello, world!";
    
    // Proxying a function object
    var target = function () { return "I am the target"; };
    var handler = {
      apply: function (receiver, ...args) {
        return "I am the proxy";
      }
    };
    
    var p = new Proxy(target, handler);
    p() === "I am the proxy";


    var handler =
    {
      // target.prop
      get: ...,
      // target.prop = value
      set: ...,
      // 'prop' in target
      has: ...,
      // delete target.prop
      deleteProperty: ...,
      // target(...args)
      apply: ...,
      // new target(...args)
      construct: ...,
      // Object.getOwnPropertyDescriptor(target, 'prop')
      getOwnPropertyDescriptor: ...,
      // Object.defineProperty(target, 'prop', descriptor)
      defineProperty: ...,
      // Object.getPrototypeOf(target), Reflect.getPrototypeOf(target),
      // target.__proto__, object.isPrototypeOf(target), object instanceof target
      getPrototypeOf: ...,
      // Object.setPrototypeOf(target), Reflect.setPrototypeOf(target)
      setPrototypeOf: ...,
      // for (let i in target) {}
      enumerate: ...,
      // Object.keys(target)
      ownKeys: ...,
      // Object.preventExtensions(target)
      preventExtensions: ...,
      // Object.isExtensible(target)
      isExtensible :...
    }
    

    Reflect API

    Full reflection API exposing the runtime-level meta-operations on objects. This is effectively the inverse of the Proxy API, and allows making calls corresponding to the same meta-operations as the proxy traps. Especially useful for implementing proxies.

    var O = {a: 1};
    Object.defineProperty(O, 'b', {value: 2});
    O[Symbol('c')] = 3;
    
    Reflect.ownKeys(O); // ['a', 'b', Symbol(c)]
    
    function C(a, b){
      this.c = a + b;
    }
    var instance = Reflect.construct(C, [20, 22]);
    instance.c; // 42

  • 相关阅读:
    快排原理讲解
    Kafka原理详解
    java中的基本数据类型转换
    centos7关闭防火墙
    安装Linux基本工具
    Kibana笔记
    虚拟机配置net模式
    2019-10-12,html+php+mysql简单留言板,作业
    2019-10-11:渗透测试,基础学习,php+mysql连接,笔记
    2019-10-10:渗透测试,基础学习,mysql语法基础,笔记
  • 原文地址:https://www.cnblogs.com/justart/p/12456501.html
Copyright © 2011-2022 走看看