null 是否在绑定函数中设置默认参数,使用 JS 绑定?

Does null set a default argument in a bound function, with JS bind?

遵守此代码:

function adder(a, b) {
  return a + b;
}
const adderFive = adder.bind(null, 5);
let pauloAgeInFuture = adderFive(41, 1) // 

console.log(pauloAgeInFuture); //->46
console.log(adderFive(5, 10)); //->10

是否忽略了第二个参数,因为 1) 因为使用 null 我是说绑定值 (5) 是默认 b arg,或者 2) 因为将 5 作为参数传递我是说函数正在等待最多还有一个论点(第一个通过)?[​​=11=]

bind() 的第一个参数是调用该函数时将提供的 this 上下文。由于您的函数不使用 this,因此您的示例中会简单地忽略此参数。

其余的参数被插入到参数列表的前面,后面是传递给绑定函数的任何参数。所以当你打电话给

adderFive(41, 1)

相当于调用

adder(5, 41, 1)

adder 只使用前两个参数,所以这个 returns 5 + 41.