相关疑难解决方法(0)

如何动态获取函数参数名称/值?

有没有办法动态获取函数的函数参数名称?

假设我的函数看起来像这样:

function doSomething(param1, param2, .... paramN){
   // fill an array with the parameter name and value
   // some other code 
}
Run Code Online (Sandbox Code Playgroud)

现在,我如何从函数内部获取参数名称及其值的列表到数组中?

javascript reflection function-parameter

282
推荐指数
14
解决办法
16万
查看次数

获取函数的默认值?

有没有办法在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该函数不起作用,因为它会在非常量的默认值上中断.

javascript default-arguments ecmascript-6

15
推荐指数
2
解决办法
743
查看次数