call、apply、bind是如何实现改变this的

call, apply, 和 bind 方法都是通过改变函数的执行上下文(this)来实现改变 this 的。

1. call 方法

  call 方法:call 方法通过将函数作为对象的方法调用,并传递一个新的上下文对象作为第一个参数来改变函数的执行上下文。函数内部可以通过 this 引用该新的上下文对象。

  • 实现示例:

Function.prototype.call = function(context, ...args) {
  context = context || window; // 如果上下文对象未提供,则默认为全局对象(浏览器中为 window)
  const key = Symbol('fn'); // 创建一个唯一的键,以避免覆盖上下文对象中的现有属性
  context[key] = this; // 将函数作为对象的方法添加到上下文对象中
  const result = context[key](...args); // 调用函数
  delete context[key]; // 删除添加的方法属性
  return result; // 返回函数执行结果
};

// 使用示例
const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

person.greet.call(otherPerson, 'Hello'); // 输出:Hello, Bob!

2. apply 方法

  apply 方法:apply 方法与 call 方法类似,不同之处在于它接受一个参数数组而不是逐个列出参数。参数数组中的每个元素都会作为参数传递给函数。

  • 实现示例:

Function.prototype.apply = function(context, args) {
  context = context || window; // 如果上下文对象未提供,则默认为全局对象(浏览器中为 window)
  const key = Symbol('fn'); // 创建一个唯一的键,以避免覆盖上下文对象中的现有属性
  context[key] = this; // 将函数作为对象的方法添加到上下文对象中
  const result = context[key](...args); // 调用函数,并将参数数组展开传递
  delete context[key]; // 删除添加的方法属性
  return result; // 返回函数执行结果
};

// 使用示例
const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

person.greet.apply(otherPerson, ['Hello']); // 输出:Hello, Bob!

3. bind 方法

  bind 方法:bind 方法返回一个新函数,并预先绑定了指定的执行上下文。新函数被调用时,它的执行上下文将是预先绑定的上下文对象。

  • 实现示例:

Function.prototype.bind = function(context, ...args) {
  const fn = this; // 当前函数
  return function(...innerArgs) {
    return fn.call(context, ...args, ...innerArgs); // 调用原始函数,并传递绑定的上下文和参数
  };
};

// 使用示例
const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

const greetBob = person.greet.bind(otherPerson);
greetBob('Hello'); // 输出:Hello, Bob!

4. 区别

4.1 call 方法:

  call 方法允许你显式地调用一个函数,并指定函数执行时的上下文(this)。除了第一个参数是要绑定的上下文对象之外,它可以接受任意数量的参数作为要传递给函数的参数。

  • 示例:

const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

person.greet.call(otherPerson, 'Hello'); // 输出:Hello, Bob!

4.2 apply 方法:

  apply 方法与 call 方法类似,但它接受一个参数数组而不是逐个列出参数。数组中的每个元素都会被作为参数传递给函数。

  • 示例:

const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

person.greet.apply(otherPerson, ['Hello']); // 输出:Hello, Bob!

4.3bind 方法:

  bind 方法不会立即调用函数,而是返回一个新函数,新函数的执行上下文(this)被绑定为指定的上下文对象。bind 方法可以预先绑定函数的上下文,并返回一个新的函数,以后可以在需要时调用。

  • 示例:

const person = {
  name: 'Alice',
  greet: function(message) {
    console.log(`${message}, ${this.name}!`);
  }
};

const otherPerson = {
  name: 'Bob'
};

const greetBob = person.greet.bind(otherPerson);
greetBob('Hello'); // 输出:Hello, Bob!

4.4 总结:

  • call 和 apply 方法可以立即调用函数并指定执行上下文,不同之处在于参数的传递方式。
  • bind 方法返回一个新函数,预先绑定了执行上下文,并可以在需要时调用。
  • call 和 apply 是直接调用函数,而 bind 是创建一个新函数。
  • call 和 apply 可以传递参数列表,而 bind 只能传递执行上下文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小新-alive

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

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

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

打赏作者

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

抵扣说明:

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

余额充值