bind创建的函数this在绑定时即固化且不可变,即使再bind、call或apply也无法更改;同时支持预设参数实现柯里化。

bind 方法创建的函数,其 this 指向在绑定时就已固化,后续无论怎么调用(包括作为事件回调、setTimeout 回调、被赋值给其他变量、甚至再次 bind),this 都不会改变。
绑定时确定,之后不可覆盖
bind 返回的新函数内部,this 被硬编码为调用 bind 时传入的第一个参数(即绑定对象)。这个绑定发生在函数创建阶段,不是执行阶段。即使把该绑定函数赋值给另一个对象的属性,或用 call/apply 显式调用,也无法改变其 this 值(call/apply 会被忽略)。
- let obj = { name: 'Alice' };
- function greet() { return `Hello, ${this.name}`; }
- let bound = greet.bind(obj);
- console.log(bound()); // "Hello, Alice"
- console.log(bound.call({ name: 'Bob' })); // 仍是 "Hello, Alice",call 无效
多次 bind 不会叠加或覆盖原始绑定
对一个已 bind 过的函数再次调用 bind,新 bind 的 this 参数会被忽略,原绑定的 this 依然生效。也就是说,bind 是“一次性永久锁定”,不是链式增强。
- let bound1 = greet.bind({ name: 'Alice' });
- let bound2 = bound1.bind({ name: 'Bob' });
- console.log(bound2()); // "Hello, Alice",不是 Bob
与 call/apply 的关键区别
call 和 apply 是立即执行并临时指定 this;bind 不执行函数,只返回一个 this 已固定的新函数。返回的函数可以延迟调用、传递、复用,且每次调用都使用同一 this。
立即学习“Java免费学习笔记(深入)”;
- greet.call({ name: 'Tom' }); // 立即执行,this 是 Tom
- let later = greet.bind({ name: 'Jerry' }); // 不执行,只是准备好了 this
- setTimeout(later, 100); // 100ms 后执行,this 仍是 Jerry
参数预设(柯里化)与 this 绑定同时生效
bind 第二个及之后的参数会作为“前置参数”被固定,和 this 一起被记入新函数。后续调用时传入的参数会追加到这些预设参数之后。
- function add(a, b, c) { return a + b + c + this.offset; }
- let context = { offset: 10 };
- let add5 = add.bind(context, 5); // 预设 a = 5
- console.log(add5(2, 3)); // 5 + 2 + 3 + 10 = 20










