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

问题描述

我希望做Python的相当于 inspect.getargspec()在Javascript。

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

我不知道,你可以从一个Javascript函数获得参数列表,但是是的名称的什么我真的很为论点从最初定义的功能。

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()然后解析出 ... 函数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函数的参数的名称/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:54