0

I know that in JS, i can use Object.keys(object) to inspect the various properties of an object, but I am wondering if there is a similar to find out what arguments a JS callback function expects.

For instance, i know the the callback function for request.get expects error, response and body respectively, but how can i find this out by using something similar to pythons dir or JS's Object.keys()

I understand that an IDE can help with this, but I am trying to find out if it is possible to do this using just JS

securisec
  • 3,435
  • 6
  • 36
  • 63
  • 1
    Check the function's `length` for the number, or if you *have* to, call `toString()` on it to see their names (hopefully it's not minified). But any script that has to do this is quite a code smell. – CertainPerformance Aug 04 '18 at 22:12
  • @CertainPerformance would you have an example on how to do this using the `request.get` example? – securisec Aug 04 '18 at 22:16
  • 1
    good IDE's intellisence will tell you what arguments are. What is your specific use case? – charlietfl Aug 04 '18 at 22:18
  • @securisec there are some examples here: https://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically -- most seem like cautionary tales to me. – Mark Aug 04 '18 at 22:25
  • Yes, this is helpful, but how would I use any of those ideas on a **callback** function? I tried using the `acorn` example, but i get back an empty array. Here is the code `request.get(url, function a(){ console.log( acorn.parse(a).body[0].params.map(x => x.name) ); })` – securisec Aug 04 '18 at 22:32

1 Answers1

0

If you're using Flow, TypeScript, or Tern, your editor/IDE can most likely provide that info for you. If you're not, you can use someFunction.toString().

Zac Anger
  • 6,983
  • 2
  • 15
  • 42