本帖最后由 icysky0605 于 2021-10-14 10:37 编辑
[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试</title>
<script>
const data = {
name: '周星驰',
action: (myname,age) => myname + age
}
Function.prototype.getName = function(){
return this.name || this.toString().match(/function\s*([^(]*)\(/)[1]
}
const paramA = 'name', paramB = 'action'
if(typeof data[paramB] === 'function'){
document.write("函数名称:"+data[paramB].getName()+"<br/>")
document.write("参数数量:"+data[paramB].length+"<br/>")
document.write("参数列表:"+getParameterName(data[paramB]).join(",")+"<br/>")
document.write("执行结果:"+data[paramB]("张三",18)+"<br/>")
}
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;
}
</script>
</head>
<body>
</body>
</html> |