0

I have a shared object named myModule contains many common-used functions:

myModule
  alert: {showAlert: ƒ}
  upload: ƒ (e)
  uuid: {createUuid: ƒ}
  __proto__: Object
  1. Function maybe child or great-grandchild of myModule
  2. These functions maybe loaded separately by multiple calls into a page

Now myModule maybe used in different script block in one page.

<script>
// loaded uuid, alert to myModule
</script>
<div> ... </div>
<script>
  myModule.alert.showAlert(s);
</script>
<div> ... </div>
<script>
// loaded upload to my Module
</script>
<div> ... </div>
<script>
  myModule.upload(f);
</script>

Now I wanna put logging at the beginning of each function of myModule. myModule is owned by my team(in different packages), I do want a way to do it with minimal change to these functions for there're so many functions. Is AOP the good choice? Or any way to make it simple?

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
LittleQ
  • 1,860
  • 1
  • 12
  • 14
  • 1
    How do you feel about wrapping all functions in myModule with a logger function: 1. `showAlert = () => alert("hi there!")` 2. `wrapper = (fn) => { console.log("log something here..."); fn() }` 3. `wrapper(showAlert)` -> export this from `myModule` – Blundering Philosopher Dec 20 '19 at 07:39
  • @BlunderingPhilosopher I decided to apply [nested Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) to myModule to wrap getter. – LittleQ Dec 26 '19 at 06:22
  • Regarding the former `AOP` tag, wrapping and reassigning already declared functionality (be it functions or methods) misses any aspect of _AOP_. Any language which wants to qualify for the latter has to provide abstraction levels for at least `Joinpoint`, `Advice` and `Aspect`. The use case described by the OP should be referred to as method modification, and JavaScript of cause is well suited for this scenario and could easily provide a complete `target`/`context` aware toolset of method modifiers like `around`, `before`, `after`, `afterThrowing` and `afterFinally` via `Function.prototype`. – Peter Seliger Sep 14 '22 at 14:26

0 Answers0