Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - 如何动态获取函数参数名称/值?(How to get function parameter names/values dynamically?)

Is there a way to get the function parameter names of a function dynamically?(有没有一种方法可以动态获取函数的函数参数名称?)

Let's say my function looks like this:(假设我的函数如下所示:) function doSomething(param1, param2, .... paramN){ // fill an array with the parameter name and value // some other code } Now, how would I get a list of the parameter names and their values into an array from inside the function?(现在,如何从函数内部将参数名称及其值的列表放入数组中?)   ask by vikasde translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following function will return an array of the parameter names of any function passed in.(以下函数将返回传入的任何函数的参数名称的数组。)

var STRIP_COMMENTS = /((//.*$)|(/*[sS]*?*/))/mg; var ARGUMENT_NAMES = /([^s,]+)/g; function getParamNames(func) { var fnStr = func.toString().replace(STRIP_COMMENTS, ''); var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); if(result === null) result = []; return result; } Example usage:(用法示例:) getParamNames(getParamNames) // returns ['func'] getParamNames(function (a,b,c,d){}) // returns ['a','b','c','d'] getParamNames(function (a,/*b,c,*/d){}) // returns ['a','d'] getParamNames(function (){}) // returns [] Edit :(编辑 :) With the invent of ES6 this function can be tripped up by default parameters.(借助ES6的发明,此功能可以通过默认参数跳闸。) Here is a quick hack which should work in most cases:(这是一个在大多数情况下都应该起作用的快速技巧:) var STRIP_COMMENTS = /(//.*$)|(/*[sS]*?*/)|(s*=[^,)]*(('(?:\'|[^' ])*')|("(?:\"|[^" ])*"))|(s*=[^,)]*))/mg; I say most cases because there are some things that will trip it up(我说大多数情况是因为有些事情会使它崩溃) function (a=4*(5/3), b) {} // returns ['a'] Edit : I also note vikasde wants the parameter values in an array also.(编辑 :我也注意到vikasde还希望数组中的参数值。) This is already provided in a local variable named arguments.(这已在名为arguments的局部变量中提供。) excerpt from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments :(摘录自https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments :) The arguments object is not an Array.(arguments对象不是数组。) It is similar to an Array, but does not have any Array properties except length.(它类似于Array,但除length外没有任何Array属性。) For example, it does not have the pop method.(例如,它没有pop方法。) However it can be converted to a real Array:(但是,可以将其转换为实际的数组:) var args = Array.prototype.slice.call(arguments); If Array generics are available, one can use the following instead:(如果可以使用Array泛型,则可以改用以下内容:) var args = Array.slice(arguments);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...