混合构造函数并将陷阱应用于Javascript Proxy对象 [英] mixing constructor and apply traps on the Javascript Proxy object

查看:88
本文介绍了混合构造函数并将陷阱应用于Javascript Proxy对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要向其中应用代理的类,可以观察方法调用和构造函数调用:

I have a class that I'd like to apply a proxy to, observing method calls and constructor calls:

Calculator.js

class Calc {
  constructor(){}

  add(a, b) {
    return a+b;
  }

  minus(a, b) {
    return a-b;
  }
}

module.exports = Calc;

index.js

const Calculator = require('./src/Calculator');

const CalculatorLogger = {
  construct: function(target, args, newTarget) {
      console.log('Object instantiated');
      return new target(...args);
  },
  apply: function(target, thisArg, argumentsList) {
      console.log('Method called');
  }
}
const LoggedCalculator = new Proxy(Calculator, CalculatorLogger);
const calculator = new LoggedCalculator();
console.log(calculator.add(1,2));

调用此命令时,我希望输出为:

When this is called, I would expect for the output to be:

实例化的对象

方法

但是,未调用apply,我认为这是因为我将Proxy附加到Calculator类,而不是实例化的对象,因此不了解apply陷阱.

however, the apply is not being called, I assume that this is because I am attaching the Proxy to the Calculator class, but not the instantiated object, and so doesn't know about the apply trap.

我如何构建一个全面的代理来观察"代理服务器?在方法调用和构造函数调用上.

How can i build an all encompassing Proxy to "observe" on method calls and constructor calls.

推荐答案

我认为这是因为我将Proxy附加到Calculator类,而不是实例化的对象,因此不了解Apply陷阱.

I assume that this is because I am attaching the Proxy to the Calculator class, but not the instantiated object, and so doesn't know about the apply trap.

完全正确,代理对对象起作用,因此除非调用Calculator类的函数属性,否则它不会调用apply,如下所示:

You are totally right, proxies act upon objects, so it won't call apply unless a function property of the Calculator class is called, as follows:

class Calculator {
  constructor() {
    this.x = 1;
  }

  instanceFunction() {
    console.log('Instance function called');
  }

  static staticFun() {
    console.log('Static Function called');
  }

}

const calcHandler = {
  construct(target, args) {
    console.log('Calculator constructor called');
    return new target(...args);
  },
  apply: function(target, thisArg, argumentsList) {
    console.log('Function called');
    return target(...argumentsList);
  }
};

Calculator = new Proxy(Calculator, calcHandler);

Calculator.staticFun();

const obj = new Calculator();

obj.instanceFunction();

很清楚,用代理包装Calculator的实例的方法可能是:

With that clear, what you could do to wrap an instance of Calculator with a proxy could be:

  1. 具有类代理以代理construct上的实例:
  1. Have the class proxy to proxify instances on construct:

const CalculatorInstanceHandler = {
  apply(target, thisArg, args) {
    console.log('Function called');
    return target(...args);
  }
}

const CalculatorClassHandler = {
  construct(target, args) {
    const instance = new target(...args);
    return new Proxy(instance, CalculatorInstanceHandler);
  }
}

  1. Calculator类中具有工厂函数以创建 proxified 实例:
  1. Have a factory function in the Calculator class in order to create proxified instances:

const CalculatorInstanceHandler = {
  apply(target, thisArg, args) {
    return target(...args);
  }
};

class Calculator {

  static getNewCalculator() {
    const instance = new Calculator();

    return new Proxy(instance, CalculatorInstanceHandler);

  }
}

这篇关于混合构造函数并将陷阱应用于Javascript Proxy对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆