13

I have simple class like this:

module.exports = class MyClass {

    function middleware() {
        console.log('call me before')
    }

    function a() {

    }
    function b() {

    }

    function c() {

    }
}

So Idea is, when someone call function a, b, c I want call middleware before execute a, b, c. How can I do it?

So, I can put middleware() to each function, but I want some dynamic way how to do this.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
kinacexoli
  • 153
  • 1
  • 6
  • if anytime you will call function a(), you cant do anything , it will only execute a. – Atul Aug 17 '18 at 21:35
  • I believe there is some way. – kinacexoli Aug 17 '18 at 21:36
  • Maybe you find something in this answer https://stackoverflow.com/questions/5033836/adding-console-log-to-every-function-automatically – villu164 Aug 17 '18 at 21:38
  • If you're willing to use backported features from ESNext, you can always use [decorators](https://tc39.github.io/proposal-decorators/). Babel has a plugin for [this](http://babeljs.io/docs/en/babel-plugin-transform-decorators). – Akshat Mahajan Aug 17 '18 at 21:43
  • Check out AOP (aspect-oriented programming), it can solve such problems. An implementetion in javascript: https://blog.bitsrc.io/aspect-oriented-programming-in-javascript-c4cb43f6bfcc – Herbertusz Dec 28 '21 at 23:56

3 Answers3

10

You could rewrite all the methods of the classes prototype by iterating over all own property names (Object.keys or for..in would not work here as class methods are not enumerable) and then replacing the original methods by a new method that calls the original method but also calls the middleware. Through that the classes behaviour doesnt change, but the middleware gets called.

 class MyClass {
    a() { console.log("a"); }
 }

 function middleware() { 
    console.log("works");
 }

 for(const key of Object.getOwnPropertyNames(MyClass.prototype)) {
     const old = MyClass.prototype[key];
     MyClass.prototype[key] = function(...args) {
       middleware(...args);
       old.call(this, ...args);
     };
 }

 (new MyClass).a();
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • totally depends on the use case , here you cant write component specfic things , or cant event bind the context . – Atul Aug 17 '18 at 21:49
6

You could use Proxy in order to trap every property access on the instance, and wrap functions.

class MyClass {
  constructor() {
    const handler = {
      get: function (obj, prop) {
        return typeof obj[prop] !== "function"
          ? obj[prop]
          : function (...args) {
              obj.middleware(prop);
              obj[prop].apply(obj, args);
            };
      },
    };
    return new Proxy(this, handler);
  }

  middleware(prop) {
    console.log(`Executing function ${prop}`);
  }

  a() {}

  b() {}

  c() {}
}

const obj = new MyClass();

obj.a();
obj.b();
obj.c();
Guerric P
  • 30,447
  • 6
  • 48
  • 86
-3

what you can is instead of calling directly function a,b,c , you can call middleware and pass the function in the parameter , middleware will call it at last.

function middleware(targetFunction) {
    console.log('call me before');
    targetFunction();
}
Atul
  • 420
  • 2
  • 10