在 JavaScript 函数的定义/执行中检查参数的名称/值 [英] Inspect the names/values of arguments in the definition/execution of a JavaScript function

查看:33
本文介绍了在 JavaScript 函数的定义/执行中检查参数的名称/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Javascript 中执行与 Python 的 inspect.getargspec() 等效的操作.

I'm looking to do the equivalent of Python's inspect.getargspec() in Javascript.

我确实知道您可以从 Javascript 函数中获取 arguments 列表,但我真正要寻找的是来自最初定义的参数的 names功能.

I do know that you can get the arguments list from a Javascript function, but what I'm really looking for is the names of the arguments from the originally defined function.

如果这实际上是不可能的,我将不得不通过获取函数的字符串来暴力"它,即 myFunc.toString() 然后解析出 ...function myFunc(...) 中.有谁知道如何以一般方式进行解析?

If this is in fact impossible, I'm going to have to 'brute-force' it by getting the string of the function, i.e. myFunc.toString() and then parsing out the ... inside of function myFunc(...). Does anyone know how to do this parsing in a general way?

感谢您的建议.

推荐答案

虽然我看不出有什么好的理由,

While I can't see any good reason for this,

var reg = /\(([\s\S]*?)\)/;
var params = reg.exec(func);
if (params) 
     var param_names = params[1].split(',');

假设 func 是你的函数名

这篇关于在 JavaScript 函数的定义/执行中检查参数的名称/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆