js 中 call、apply、bind 方法的原理与应用

方法 call apply bind
多个 单个数组 多个
作用 函数调用改变this 同理 同理
直接执行的 直接执行的 返回待执行函数

基本概念

callapplybind 是 Function 对象的三个方法,用于改变函数执行时的 this 指向:

javascript 复制代码
func.call(thisArg, param1, param2, ...)
func.apply(thisArg, [param1, param2, ...])
func.bind(thisArg, param1, param2, ...)
  • 浏览器里,在全局范围内this指向window对象
  • 函数中,this永远指向最后调用他的那个对象;
  • 构造函数中,this指向new出来的那个新的对象

  • call、apply、bind中的this被强绑定在指定的那个对象上;
  • 箭头函数中this比较特殊,箭头函数this为父作用域的this,不是调用时的this.要知道前四种方式,都是调用时确定,也就是动态的,而箭头函数的this指向是静态的,声明的时候就确定了下来;
  • apply、call、bind都是js给函数内置的一些API,调用他们可以为函数指定this的执行,同时也可以传参。

this 指向规则

  1. 全局范围内:this 指向 window 对象
  2. 函数中:this 指向最后调用它的对象
  3. 构造函数中:this 指向 new 出来的新对象
  4. call/apply/bind:this 被强绑定在指定对象上
  5. 箭头函数:this 为父作用域的 this(静态绑定)

方法区别

方法 参数形式 执行时机 返回值
call 参数列表 立即执行 函数返回值
apply 参数数组 立即执行 函数返回值
bind 参数列表 不立即执行 返回绑定函数

应用场景

1. 判断数据类型

javascript 复制代码
function getType(obj) {
  let type = typeof obj;
  if (type !== "object") return type;
  return Object.prototype.toString.call(obj).replace(/^$/, '$1');
}

2. 类数组借用数组方法

javascript 复制代码
var arrayLike = {
  0: 'java',
  1: 'script',
  length: 2
}

Array.prototype.push.call(arrayLike, 'jack', 'lily');
// {0: "java", 1: "script", 2: "jack", 3: "lily", length: 4}

3. 获取数组最大/最小值

javascript 复制代码
let arr = [13, 6, 10, 11, 16];
const max = Math.max.apply(Math, arr);
const min = Math.min.apply(Math, arr);

手动实现

实现 bind

javascript 复制代码
Function.prototype.myBind = function(context) {
  if (typeof this !== 'function') throw new TypeError('Error');
  
  const _this = this;
  const args = [...arguments].slice(1);
  
  return function F() {
    if (this instanceof F) {
      return new _this(...args, ...arguments);
    }
    return _this.apply(context, args.concat(...arguments));
  }
}

实现 call

javascript 复制代码
Function.prototype.myCall = function(context) {
  context = context || window;
  context.fn = this;
  
  const args = [...arguments].slice(1);
  const result = context.fn(...args);
  
  delete context.fn;
  return result;
}

实现 apply

javascript 复制代码
Function.prototype.myApply = function(context = window, ...args) {
  const key = Symbol('key');
  context[key] = this;
  
  const result = context[key](args);
  delete context[key];
  
  return result;
}

总结

这三个方法的核心原理都是通过改变函数的 this 指向来实现"借用"方法的功能。在实际开发中,它们常用于:

  • 改变函数上下文
  • 实现函数借用
  • 实现柯里化(bind)
  • 处理类数组对象
  • 实现更灵活的函数调用方式
相关推荐
arvin_xiaoting4 小时前
OpenClaw学习总结_I_核心架构_8:SessionPruning详解
前端·chrome·学习·系统架构·ai agent·openclaw·sessionpruning
工程师老罗5 小时前
Image(图像)的用法
java·前端·javascript
swipe6 小时前
把 JavaScript 原型讲透:从 `[[Prototype]]`、`prototype` 到 `constructor` 的完整心智模型
前端·javascript·面试
问道飞鱼6 小时前
【前端知识】React 组件生命周期:从底层原理到实践场景
前端·react.js·前端框架·生命周期
CHU7290357 小时前
定制专属美丽时刻:美容预约商城小程序的贴心设计
前端·小程序
浩~~7 小时前
反射型XSS注入
前端·xss
AwesomeDevin7 小时前
AI时代,我们的任务不应沉溺于与 AI 聊天,🤔 从“对话式编程”迈向“数字软件工厂”
前端·后端·架构
harrain7 小时前
antvG2折线图和区间range标记同时绘制
前端·javascript·vue.js·antv·g2
德育处主任Pro8 小时前
从重复搭建到高效生产,RollCode的H5开发新范式
前端
蜡台8 小时前
SPA(Single Page Application) Web 应用(即单页应用)架构模式 更新
前端·架构·vue·react·spa·spa更新