Get function parameter names in JavaScript

I have a JavaScript function that takes a function as a parameter, for example:

var myFunction = function(funParameter) { // funParameter is a function }; 

I can call this function as follows:

 myFunction(function (aParameter, anotherOne) { // do stuff }); 

Inside the body of myFunction , how can I get the parameters that funParameter should receive? I want to know the parameters declared using the function passed to myFunction (in the above case, I want to know that the parameter function accepts aParameter and anotherOne .

The only way I know is to correctly funParameter.toString() , but I feel like it's hacking.

It should be like in Mocha tests:

 it('should test something synchronously', function () {...}); it('should test something asynchronously', function (done) { // test... done(); }); 

You should behave differently if the function you pass to it accepts the done parameter or not.

+4
source share
3 answers

What you want is this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length

If the parameter names are guaranteed not to be consistent throughout the code and will never be softened in any way, you do not want to check whether this parameter is assigned, "done". In most cases, developers should have the right to name their functional parameters.

In addition, javascript code (for the browser) is usually faded and distorted, and parameter names are shortened. If the names are changed, this will violate your function.

However, what you most likely want to do is decide how many parameters are set by the function, and what can be done with Function.length .

However, this is also not a reliable solution, because you can always specify nothing and still get parameter values โ€‹โ€‹with arguments .

In JavaScript, it is usually bad to assume that something will work as I intended. Because you never know who will do what with their codes.

The language itself is so dynamic in different ways, you have to be very careful before creating your functions / APIs.

from MDN:

 console.log( (function () {}).length ); /* 0 */ console.log( (function (a) {}).length ); /* 1 */ console.log( (function (a, b) {}).length ); /* 2 etc. */ console.log( (function (...args) {}).length ); /* 0, rest parameter is not counted */ 
+3
source

If you are looking specifically for how Mocha does this, check out the source code .

I dig into a bit and find that Mocha just checks the length property of the passed function, as you see in this source file . They then perform an if check on the async property, as shown below. If async is true, it calls the test function differently to handle the asynchronous nature of the test function.

 this.async = fn && fn.length 
+1
source

Getting function parameters with Function.prototype.toString really hacky. Until now, although it was not in the specifications, it is very well supported by all the browsers I tested, and is used by AngularJS to match the requested controller capabilities by the unordered parameter name.

 function myFunction(func) { var params = func.toString() .match(/function\s*.*\((.+)\)/)[1] .split(','); return params; // ['aParameter', 'anotherOne'] } 

* source changed from 'functools'

0
source

Source: https://habr.com/ru/post/1500618/


All Articles