JavaScript 开发 - 获取函数名称、获取函数参数数量、获取函数参数名称

一、获取函数名称

  1. 通过 name 属性获取函数名称
js 复制代码
function receiver(fn) {
    console.log(fn.name);                
}

console.log("----- 箭头函数 -----");


const test1 = () => {};

console.log(test1.name);
receiver(test1);

console.log("----- 普通函数 -----");

function test2() {}

console.log(test2.name);
receiver(test2);

const test3 = function test_3() {};

console.log(test3.name);
receiver(test3);

console.log("----- 匿名函数 -----");

const test4 = function () {};

console.log(test4.name);
receiver(test4);
复制代码
# 输出结果

----- 箭头函数 -----
test1
test1
----- 普通函数 -----
test2
test2
test_3
test_3
----- 匿名函数 -----
test4
test4
js 复制代码
function receiver(fn) {
    console.log(fn.name);                
    console.log(fn.name === "");
}

receiver(() => {});
复制代码
# 输出结果


true
  1. name 属性是只读的,不能用赋值运算符修改
js 复制代码
function test() {
    console.log("execute test");
}

console.log(test.name);
test();

test.name = "test123"
console.log(test.name);
test();
复制代码
# 输出结果

test
execute test
test
execute test

二、获取函数参数数量

js 复制代码
function func1() {}

console.log(func1.length);

function func2(a, b) {}

console.log(func2.length);

function fun3(name, age = 18, ...rest) {}

console.log(fun3.length);
复制代码
# 输出结果

0
2
1

三、获取函数参数名称

js 复制代码
function test1(a, b, c) {
    console.log(a, b, c);
}

console.log(test1.toString());

const test2 = (x, y, z) => {};

console.log(test2.toString());

function test3(name, age = 18, ...rest) {}

console.log(test3.toString());
复制代码
function test1(a, b, c) {
				console.log(a, b, c);
			}
function param name.html:19 (x, y, z) => {}
function param name.html:23 function test3(name, age = 18, ...rest) {}
js 复制代码
function getParamNames(fn) {
    const fnStr = fn.toString();

    // 匹配参数部分(支持箭头函数和普通函数)
    const paramMatch = fnStr.match(/\(([^)]*)\)/);
    if (!paramMatch) return [];

    // 分割参数并清理空格
    const result = paramMatch[1]
        .split(",")
        .map((param) => param.trim())
        .filter((param) => param);

    for (let i = 0; i < result.length; i++) {
        const param = result[i];
        if (param.includes("=")) {
            result[i] = param.split("=")[0].trim();
            continue;
        }
        if (param.startsWith("...")) {
            result[i] = param.slice(3).trim();
            continue;
        }
    }

    return result;
}

function test1(a, b, c) {
    console.log(a, b, c);
}

console.log(getParamNames(test1));

const test2 = (x, y, z) => {};

console.log(getParamNames(test2));

function test3(name, age = 18, ...rest) {}

console.log(getParamNames(test3));
复制代码
# 输出结果

(3) ['a', 'b', 'c']
(3) ['x', 'y', 'z']
(3) ['name', 'age', 'rest']
相关推荐
sbjdhjd3 小时前
Redis 主从复制、哨兵高可用与 Cluster 集群部署实验手册
运维·前端·redis·云原生·开源·bootstrap·html
z落落3 小时前
C# 泛型方法(原理、类型推断、多泛型参数)+泛型效率(普通类型 VS Object装箱 VS 泛型)
开发语言·c#
L_09073 小时前
【C++】异常
开发语言·c++
乐兮创想 小林4 小时前
企业官网移动端性能优化实战:从 Core Web Vitals 到图片/CDN/响应式的工程清单
前端·性能优化·网站建设·北京网站建设公司
疯狂SQL4 小时前
JWT 在线解码、验签、生成一篇讲透:附前端实现、工具架构与在线体验地址
javascript·jwt·编解码·jwt测试
前端一小卒4 小时前
不手写代码的第 30 天,我才明白前端这个岗位还剩什么
前端·javascript·ai编程
Ajie'Blog4 小时前
Copilot Agent Tasks API 开放:AI 编程开始进入后台任务时代
服务器·前端·javascript·人工智能·copilot·ai编程
世辰辰辰4 小时前
批量修改图片/文本名子
开发语言·python·批量修改文件名
老毛肚5 小时前
jeecgboot vue TS & 模板化 04
前端·javascript·vue.js
晓13135 小时前
【Cocos Creator 2.x】篇——第二章 入门
javascript·游戏引擎