有没有办法动态获取函数的函数参数名称?
假设我的函数看起来像这样:
function doSomething(param1, param2, .... paramN){
// fill an array with the parameter name and value
// some other code
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何从函数内部获取参数名称及其值的列表到数组中?
有没有办法在JavaScript中检索函数的默认参数值?
function foo(x = 5) {
// things I do not control
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获得x这里的默认值?最理想的是,像:
getDefaultValues(foo); // {"x": 5}
Run Code Online (Sandbox Code Playgroud)
请注意,toString该函数不起作用,因为它会在非常量的默认值上中断.