0

Suppose I have several arrow functions:

const a = (b, c) => { // body
}

const b = (g, f) => { // body
}

module.exports = { a }

Now I'd like to have references to all this module's functions (not only exported ones as 'a' function but 'b' function also) How to make something like const functionsArray = module...keys() (what should be here?) How to do this inside this module?

skl
  • 103
  • 7
  • 2
    Why only arrow functions? – Barmar Mar 22 '21 at 22:33
  • Related: https://stackoverflow.com/questions/28222228/javascript-es6-test-for-arrow-function-built-in-function-regular-function – Barmar Mar 22 '21 at 22:33
  • 1
    What environment are you working in? It's impossible to inspect the scope of a module from outside. Are you getting the module code as a string and want to statically analyse it? What is your goal, why do you want to do this? – Bergi Mar 22 '21 at 22:34
  • @Barmar Perhaps most complicated case. (I use them often) – skl Mar 22 '21 at 22:35
  • Why do you think arrow functions would be any more complicated to find than other functions? – Barmar Mar 22 '21 at 22:36
  • If there's a way to get all the names defined in a module, loop over them and check whether `typeof x == "function"` – Barmar Mar 22 '21 at 22:37
  • @Bergi I'd like to get this functions list inside module. – skl Mar 22 '21 at 22:38
  • @Barmar That'll do :) But I don't know how – skl Mar 22 '21 at 22:39
  • @Bergi I need this to automatically make proxies for them (for logging) – skl Mar 22 '21 at 22:46
  • @skl Don't use proxies, use wrapping decorators. Apply them explicitly on each function you want to log - `const a = withLogging("a", (b, c) => { … });`. There is no other way to achieve this dynamically from within the module code, especially with `const` declarations. If you need this to be automatic, you will need to use a transpiler (static code transformation) to inject the logging code. – Bergi Mar 22 '21 at 23:34
  • @skl have you found the solution? – Konrad Aug 09 '22 at 17:22

0 Answers0