How to use JavaScript to obtain all function parameter names _ javascript skills

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.