call apply bind原理和使用

callapplybind 是 JavaScript 中用来显式改变函数 this 指向的方法。它们主要用于函数上下文的转换或借用其他对象的方法。以下是它们的详细解释和使用方法:


1. call 方法

语法
func.call(thisArg, arg1, arg2, ...);
  • thisArg:指定 this 的值。
  • arg1, arg2, ...:要传给函数的参数列表。
特点
  • 立即执行 函数。
  • 参数是逐个传递的。
示例
function greet(greeting, punctuation) {
  console.log(`${greeting}, ${this.name}${punctuation}`);
}

const person = { name: 'Alice' };
greet.call(person, 'Hello', '!');  // 输出: Hello, Alice!

2. apply 方法

语法
func.apply(thisArg, [argsArray]);
  • thisArg:指定 this 的值。
  • argsArray:将参数作为一个数组传递。
特点
  • 立即执行 函数。
  • 参数必须以数组或类数组形式传递。
示例
function greet(greeting, punctuation) {
  console.log(`${greeting}, ${this.name}${punctuation}`);
}

const person = { name: 'Bob' };
greet.apply(person, ['Hi', '.']);  // 输出: Hi, Bob.
使用场景

当参数是动态生成的数组时,apply 会更方便。例如:

const numbers = [1, 2, 3, 4, 5];
console.log(Math.max.apply(null, numbers));  // 输出: 5

3. bind 方法

语法
const boundFunc = func.bind(thisArg, arg1, arg2, ...);
  • thisArg:指定 this 的值。
  • arg1, arg2, ...:预先传入的参数(可选)。
特点
  • 不立即执行,返回一个新函数,this 已绑定到指定对象。
  • 可以实现 柯里化(提前绑定部分参数)。
示例
function greet(greeting, punctuation) {
  console.log(`${greeting}, ${this.name}${punctuation}`);
}

const person = { name: 'Charlie' };
const boundGreet = greet.bind(person, 'Hey');
boundGreet('!');  // 输出: Hey, Charlie!

对比总结

特性callapplybind
立即执行
参数形式逐个传递数组传递逐个传递(预绑定部分参数)
返回值函数执行结果函数执行结果绑定后的新函数

实际使用场景

借用方法

当一个对象没有某方法时,可以借用其他对象的方法来使用。

const obj = { name: 'David' };
const printName = function() { console.log(this.name); };

printName.call(obj);  // 输出: David
伪数组转数组

使用 Array.prototype.sliceapply,可以将类数组(如 arguments)转换为真正的数组:

function toArray() {
  return Array.prototype.slice.apply(arguments);
}
console.log(toArray(1, 2, 3));  // 输出: [1, 2, 3]
事件绑定的 this 问题

在事件处理函数中,使用 bind 来确保 this 指向正确的对象:

class Button {
  constructor(name) {
    this.name = name;
  }
  click() {
    console.log(`Button clicked: ${this.name}`);
  }
}

const btn = new Button('Submit');
document.querySelector('#myButton').addEventListener('click', btn.click.bind(btn));

代码练习

  1. callapply 的对比
function sum(x, y) {
  return x + y;
}

console.log(sum.call(null, 2, 3));     // 输出: 5
console.log(sum.apply(null, [2, 3])); // 输出: 5
  1. 使用 bind 实现柯里化
function multiply(a, b) {
  return a * b;
}

const double = multiply.bind(null, 2);
console.log(double(4));  // 输出: 8

总结

  • call:立即调用函数,参数逐个传递。
  • apply:立即调用函数,参数以数组形式传递。
  • bind:返回绑定了 this 的新函数,可延迟调用。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鎈卟誃筅甡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值