如何在不调用函数的情况下向函数实参添加参数

How to add parameters to a function argument without calling it

本文关键字:函数 实参 添加 参数 情况下 调用      更新时间:2023-09-26

例如,addEventListener的第二个参数有一个"e"或"evt"或"event"或"…"形参。我如何添加参数到一个函数参数而不调用它(不使用参数对象)。这可能吗?

抱歉我的英语不好

您正在寻找部分函数

可以创建带参数的函数而不提供参数。例如,

function add(x, y) {
    return x + y;
}
console.log(add(1, 2)); // 3
console.log(add(2));    // NaN (2 + undefined)
console.log(add(0));    // NaN (undefined + undefined)

如果你不想让函数返回像NaN这样的值,你可以这样写:

function add2(x, y) {
    x = x || 0;
    y = y || 0;
    return x + y;
}
console.log(add2(1, 2)); // 3
console.log(add2(2));    // 2
console.log(add2(0));    // 0