前端每日一练:问题:bind,apply,call 的区别,及内在实现手写

call 方法:​

  • 用于调用一个函数,显式指定函数内部的 this 指向,参数以列表的形式传递给函数。​
  • 语法:func.call(thisArg, arg1, arg2, ...)​
  • 直接调用函数,立即执行。

用法 与 模拟实现

Function.prototype.myCall = function (context, ...args) {​
  context = context || window; // 如果没有传入上下文,则默认为全局对象​
  const uniqueID = Symbol(); // 创建一个唯一的键,以避免属性名冲突​
  context[uniqueID] = this; // 在上下文中添加一个属性,将函数赋值给这个属性​
  const result = context[uniqueID](...args); // 调用函数​
  delete context[uniqueID]; // 删除属性​
  return result; // 返回函数执行的结果​
};​
​
function greet(message) {​
  console.log(`${message}, ${this.name}!`);​
}​
​
const person = {​
  name: 'Alice',​
};​
​
greet.myCall(person, 'Hello'); // 输出 "Hello, Alice!"​
​
// 原生方法​
greet.call(person, 'Hello'); // 输出 "Hello, Alice!"

apply 方法:​

  • 用于调用一个函数,显式指定函数内部的 this 指向,参数以数组的形式传递给函数。​
  • 语法:func.apply(thisArg, [arg1, arg2, ...])​

用法 与 模拟实现:

Function.prototype.myApply = function (context, args) {​
  context = context || window;​
  const uniqueID = Symbol();​
  context[uniqueID] = this;​
  const result = context[uniqueID](...args);​
  delete context[uniqueID];​
  return result;​
};​
​
function greet(message) {​
  console.log(`${message}, ${this.name}!`);​
}​
​
const person = {​
  name: 'Alice',​
};​
​
greet.myApply(person, ['Hi']); // 输出 "Hi, Alice!"​
​
// 原生方法​
greet.apply(person, ['Hi']);  // 输出 "Hi, Alice!"

bind 方法:​

  • bind 方法不会立即调用函数,而是创建一个新的函数,该函数的 this 指向由 bind 的第一个参数指定,参数以列表的形式传递给函数。​
  • 语法:newFunc = func.bind(thisArg, arg1, arg2, ...)​
  • 不会立即执行函数,而是返回一个新函数。​
  • 用法 与 模拟实现:
    Function.prototype.myBind = function (context, ...args) {​
      const func = this;​
      return function (...newArgs) {​
        return func.apply(context, args.concat(newArgs));​
      };​
    };​
    ​
    function greet(message) {​
      console.log(`${message}, ${this.name}!`);​
    }​
    ​
    const person = {​
      name: 'Alice',​
    };​
    ​
    const myBoundGreet = greet.myBind(person, 'Hey');​
    myBoundGreet(); // 输出 "Hey, Alice!"​
    ​
    // 原生方法​
    const boundGreet = greet.bind(person, 'Hey');​
    boundGreet(); // 输出 "Hey, Alice!"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值