获取用于接口目的的函数参数名称 [英] Get function parameter names for interface purposes

查看:45
本文介绍了获取用于接口目的的函数参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何检索函数参数名称吗?例如:

can anyone help me on how to retrieve function parameter names? For example:

var A = function(a, b, c) {};

我需要获取函数外部的参数名称,分别为 a b c (从字面上看,例如放在[[ a "," b "," c "]]数组中),这样我不仅可以验证面向接口的类中的对象/类属性,还有参数.

I need to get the parameter names outside the function, which is a, b, and c (as in literally as strings, such as to be put in an array ["a", "b", "c"]), so I can validate not only object/class properties in a class towards an interface, but also the parameters.

在这个问题开始引起混乱之前,让我先解释一下我所用的接口实现以及为什么需要上述问题的答案的原因:

Before this question starts raising confusion, let me explain about interface implementation in my term and the reason why I need the answer of the question above:

首先,我找到了一种我想在Glen Ford在JavaScript中实现接口的有趣方式:

First of all, I found one interesting way I would like to use in implementing interface by Glen Ford in JavaScript:

for (var member in theInterface) {
    if ( (typeof theObject[member] != typeof theInterface[member]) ) {
        alert("object failed to implement interface member " + member);
        return false;
    }
}

(请参见但是,上面的实现仅验证对象属性(变量和方法),这对我来说还不够.

However, the implementation above only validates the object properties (variable and methods) only, which is not enough for me.

我需要在参数级别进行更多验证,以便实现一个接口的代码应遵循接口方法,并且还应遵循接口方法中的参数.

I need more validation to parameter level, so that the code that implements one interface should follow the interface methods and also follow the parameters from the interface methods as well.

PS.请不要争论为什么使用接口,或者为什么它无用或有用;相反,如果您可以提供更好的接口实现,则它最有用.这里的重点是我只需要知道如何获取函数参数名称.

PS. Please don't argue about why use interface or why it's useless or useful; instead it's most useful if you can provide better interface implementation. The focus here is I just need to know how to get function parameter names.

推荐答案

我怀疑是否有任何合适的方法,因为javascript参数值在传递时可以具有不同的类型,类型无关紧要,因此顺序

I doubt that there is any decent way to this, in javascript parameter values can be of different type when passed, the types doesn't matter but the order thus.

这可能不是您要查找的答案,但这至少可以有所帮助.

This might not be the answer that you are looking for but this could help at least.

function test(a,b,c,d) {

}

//this will give us the number of parameters defined in the a function.
confirm(test.length); 

要真正知道名称,可以解析函数定义

To really know the names you can parse the function definition

  var str = window['test'].toString();
   //str will be "function test(a,b,c,d){}"

下面是我做的一个简单的解析测试:

Below is a simple parsing test I made:

function test(a,b,c,d)
{

}

var b = function(x,y,  z){};
alert(getFnParamNames(b).join(","));
alert(getFnParamNames(test).join(","));

function getFnParamNames(fn){
    var fstr = fn.toString();
    return fstr.match(/\(.*?\)/)[0].replace(/[()]/gi,'').replace(/\s/gi,'').split(',');
}

这篇关于获取用于接口目的的函数参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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