简体   繁体   中英

Print all parameter name of a Javascript Function

I have a javascript file in which I have defined a custom function which take some input object and perform some action.
This file is called from many files.
Now I want to priint all the params name used in this function.

May be I need to read whole file as a string and print the desired output

 function customFunction(param){
       if(param.isOK == yes){}
       if(param.hasMenu == no){}
       ..
       ..
       ..
       if(param.cancelText == "cancel"){}

    }

I want to write a program which reads this file and output all the params name. eg

isOK | hasMenu | ...... | CancelText ..

You can use Regular Expression .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace _32666940
{

    class Program
    {
        public static String str = "function customFunction(param){ " +
            "if(param.isOK == yes){}" +
            "if(param.hasMenu == no){}" +
            "if(param.cancelText == \"cancel\"){}" +    // I had to add \ before "
            "if(param[  'isOK'  ] == yes){}" +
            "if(param[\"isOK\"] == yes){}" +
            "}";
        static void Main()
        {
            //@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*"
            Regex regex = new Regex(@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*");
            Match match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            regex = new Regex(@"\bparam\[\s*(['""])(.+?)\1\s*]");
            match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            Console.ReadKey();

        }
    }
}

Edit: updated the code to match param['some-key']

Edit updated the code to match param["some-key"]

If param is object, then you can use jQuery each() method. $.each() is an object iterator utility.

You can loop through this object using each() method as following

$.each( param, function( key, value ) {
     console.log(value);
    //Add your logic as per requirement
});

It will print all the values(in console) available in the param object.

Ref: jQuery each()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM