
Lv.1
()
签名:写代码()()() 玩起床()()() 什(
在 一个萌新的睿·智js代码问题 中回复
我只知道可以用下面的代码获取函数的所有参数名
function getParameterName(fn) {
if(typeof fn !== 'object' && typeof fn !== 'function' ) return;
const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
const DEFAULT_PARAMS = /=[^,)]+/mg;
const FAT_ARROWS = /=>.*$/mg;
let code = fn.prototype ? fn.prototype.constructor.toString() : fn.toString();
code = code
.replace(COMMENTS, '')
.replace(FAT_ARROWS, '')
.replace(DEFAULT_PARAMS, '');
let result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g);
return result === null ? [] :result;
}2022-08-22T10:14:45 点赞:0