Is it possible to reflect the arguments of a Javascript function?

Is it possible to get all the arguments so that the javascript function is written to ? (I know that all arguments to the Javascript function are "optional")? If not, is it possible to get the number of arguments? For example, in PHP you can use:

$class = new ReflectionClass('classNameHere'); $methods = $class->getMethods(); foreach ($methods as $method) { print_r($method->getParameters()); } 

... or something like that, I haven't touched PHP after a while, so the above example may be wrong.

Thanks in advance!

EDIT: Unfortunately, I should be able to get arguments outside the function body ... Sorry for the lack of clarification, but thanks for the current answers!

+21
javascript reflection methods
Aug 03 '11 at 4:21
source share
7 answers

Suppose your function name is foo

Is it possible to get all the arguments so that the Javascript function is written to accept?

arguments[0] to arguments[foo.length-1]

If not, is it possible to get the number of arguments?

foo.length will work

+10
Aug 03 2018-11-11T00:
source share

This new version also supports arrow functions ...

 args = f => f.toString ().replace (/[\r\n\s]+/g, ' '). match (/(?:function\s*\w*)?\s*(?:\((.*?)\)|([^\s]+))/). slice (1,3). join (''). split (/\s*,\s*/); function ftest (a, b, c) { } let aftest = (a, b, c) => a + b / c; console.log ( args (ftest), // = ["a", "b", "c"] args (aftest), // = ["a", "b", "c"] args (args) // = ["f"] ); 

Here is what I think you are looking for:

  function ftest (a, b, c) { } var args = ftest.toString (). replace (/[\r\n\s]+/g, ' '). match (/function\s*\w*\s*\((.*?)\)/)[1].split (/\s*,\s*/); 

args will be an array of test argument names ie ['a', 'b', 'c']

The args value will be an array of parameter names if ftest is a function. The array will be empty if ftest has no parameters. The args value will be null if ftest does not match the regular expression, i.e. this is not a function.

+41
Aug 03 2018-11-11T00:
source share

can get all the formal javascript parameter name:

 var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function formalParameterList(fn) { var fnText,argDecl; var args=[]; fnText = fn.toString().replace(STRIP_COMMENTS, ''); argDecl = fnText.match(FN_ARGS); var r = argDecl[1].split(FN_ARG_SPLIT); for(var a in r){ var arg = r[a]; arg.replace(FN_ARG, function(all, underscore, name){ args.push(name); }); } return args; } 

this can be tested as follows:

  var expect = require('expect.js'); expect( formalParameterList(function() {} )).to.eql([]); expect( formalParameterList(function () {} )).to.eql([]); expect( formalParameterList(function /* */ () {} )).to.eql([]); expect( formalParameterList(function (/* */) {} )).to.eql([]); expect( formalParameterList(function ( a, b, c ,d /* */, e) {} )).to.eql(['a','b','c','d','e']); 

Note: This method is used with the $ AngularJs injector and is implemented in the annotation function. (see https://github.com/angular/angular.js/blob/master/src/auto/injector.js and the corresponding unit test in https://github.com/angular/angular.js/blob/master /auto/injectorSpec.js )

+18
Dec 01 '12 at 14:59
source share

Check only required characters. with func.toString (). regex you checked the full length .so if the class function with 500 lines of code ...

 function getParams(func){ var str=func.toString(); var len = str.indexOf("("); return str.substr(len+1,str.indexOf(")")-len -1).replace(/ /g,"").split(',') } 
+2
Sep 24 '13 at 22:06
source share

Now that you are talking outside the function body, I can only imagine that you want to know what parameter names are? Since, as far as the values ​​go, you already know what arguments you are passing. Other answers said that you can get the length of the function, which is the number of parameters that it explicitly declares. Now, if you want to know the names outside the function, what about toString hack?

Consider

 function f(oh, hi, there) { return hi + there / oh; } 

Then

 alert(f); 

What do you see? RIght, just rename them! Okay, sorry to understand this. This may not be standard ECMAScript, but it works in Chrome ...

+1
Aug 03 2018-11-11T00:
source share

The answer to HBP is what most people are looking for, but if you define a function, you can also assign a property to a function object. For example,

 a.arguments = ['foo', 'bar', 'baz'] function a(foo, bar, baz) { // do stuff } 

This is more understandable, but you will have to write your arguments twice.

0
Jun 02 '16 at 16:16
source share

JavaScript is ECMAScript dialects, according to the ECMAScript standard, the function is also an object, and when the function is called, the function can access the arguments object, this argument is an array type object, it has the length property, so you can use arguments.length to move all arguments passed to this function. visit http://interglacial.com/javascript_spec/a-13.html#a-13.2.1 for more details.

-one
Aug 03 2018-11-11T00:
source share



All Articles