This article uses javascript to retrieve all function parameter names. If you are interested in js to retrieve all function parameter names, let's learn about it. I wrote a JavaScript function to parse the function parameter names, the Code is as follows:
Function getArgs (func) {// first use regular expression matching to obtain a string that matches the parameter pattern. // The first group is this: ([^)] *) any character without right brackets var args = func. toString (). match (/function \ s. *? \ ([^)] *) \)/) [1]; // use commas to separate parameters (arguments string ). return args. split (","). map (function (arg) {// remove annotation (inline comments) and space return arg. replace (/\/\*. *\*\//,""). trim ();}). filter (function (arg) {// make sure there is no undefined. return arg ;});}
The above is the detected function. The sample code is as follows:
function myCustomFn(arg1, arg2,arg3) { // ...}// ["arg1", "arg2", "arg3"]console.log(getArgs(myCustomFn));
Is a regular expression a good thing? I don't know anything about it, but it is very useful in appropriate scenarios!
With a Java method to get the current function name: Java obtains the name of the current function in the function
public class Test { private String getMethodName() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); StackTraceElement e = stacktrace[2]; String methodName = e.getMethodName(); return methodName; } public void getXXX() { String methodName = getMethodName(); System.out.println(methodName); } public void getYYY() { String methodName = getMethodName(); System.out.println(methodName); } public static void main(String[] args) { Test test = new Test(); test.getXXX(); test.getYYY(); } }
[Running result]
GetXXX
GetYYY
Note]
Line 2 of code, stacktrace [0]. getMethodName () is getStackTrace, stacktrace [1]. getMethodName () is getMethodName, stacktrace [2]. getMethodName () is the name of the function that calls getMethodName.
// Note: Position in stacktrace;
// [1] is "getMethodName", and [2] is the method used to call this method
public static String getMethodName() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); StackTraceElement e = stacktrace[2]; String methodName = e.getMethodName(); return methodName; }
The above content is the method for getting all function parameter names in js described in this article. If this article is not well written, please forgive me. Thank you for your valuable comments.