打印Javascript函数的所有参数名称

时间:2015-09-19 10:08:58

标签: javascript c# data-structures

我有一个javascript文件,我在其中定义了一个自定义函数,它接受一些输入对象并执行一些操作 从多个文件中调用此文件 现在我想修改此函数中使用的所有params名称。

可能我需要将整个文件作为字符串读取并打印所需的输出

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

    }

我想编写一个读取此文件并输出所有参数名称的程序。 例如

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

2 个答案:

答案 0 :(得分:2)

您可以使用Regular Expression

WebViewClient

修改:更新代码以匹配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(); } } }

修改更新了代码以匹配param['some-key']

答案 1 :(得分:0)

如果param是object,那么你可以使用jQuery each()方法。 $.each()是一个对象迭代器实用程序。

您可以使用each()方法循环遍历此对象,如下所示

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

它将打印param对象中可用的所有值(在控制台中)。

参考:jQuery each()

相关问题