0

I want a generic way to find out the required parameters by a any javascript function including natives ones, but I'm unable to find one. I've seen this stackoverflow link, but it works only for functions that we define. toString() doesn't tell us about the function parameters of native functions like window.setTimeout() :

How to get function parameter names/values dynamically from javascript

I'm specifically interested in the GreaseMonkey methods like GM_setValue etc. , which behave similarly and don't show there parameters when used with toString() (I want to validate that a method passed to me has two parameters)

Community
  • 1
  • 1
Piyush Soni
  • 1,356
  • 3
  • 17
  • 40
  • 1
    There's no such thing as a required argument in JS. A function can take a number of named arguments. The code inside it can *treat* those as required or optional. It can access the arguments through the `arguments` object and make arguments required without giving them names in the first place. – Quentin Feb 25 '15 at 15:00
  • @Quentin: Yes, I know that it's up to us whether we want to send all parameters or not, but the specific case I mentioned, GM_setValue requires both parameters for it to function as desired. Or, setTimeout requires at least two parameters to function properly. That's what I meant by 'required' in my question. – Piyush Soni Feb 25 '15 at 15:33
  • There's no way to tell that. – Quentin Feb 25 '15 at 16:28

1 Answers1

2

You cannot do this.

You can check how many named parameters a function accepts by inspecting the function's length property:

(function testFn (arg1, arg2) {}).length // Will be 2

Or you can convert the function's body into string and parse the text.

However, a named function parameter does not correlate in any way to a required parameter. Consider the following:

function testFn (arg1, arg2) {
  arg1 = arg1 || 'some default'
  arg2 = arg2 || true
}

// Both work fine
testFn() // No args passed
testFn('customValue', false) // Args passed

function anotherFn () {
  var arg1 = arguments[0] // Discouraged! Just an example
  arg1.toString() // Do stuff with a required arg1 argument
}

// This will throw at you!
anotherFn()

Instead of validating that a function has two arguments, simply validate that the function behaves to your expectations in a test. In general, though, you should only test that what you just got is indeed a function and then simply call it. The responsibility of using the arguments you give to it should be ensured at the receiver (the function you just called), not in the caller.

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
  • Yes, I understand that we can skip the arguments in a function which expects them, and pass them in a function which doesn't. But, there will be a function "definition/declaration" somewhere for that method. I just need that function declaration. It works for the functions I define, but not for Javascript's native methods. I just wanted to know if there's a way to check that. – Piyush Soni Feb 25 '15 at 19:31