当前位置: 首页 > news >正文

JavaScript 中 apply、call 和 bind 方法的手写实现

JavaScript 中 apply、call 和 bind 方法的手写实现
call 的实现

与 apply 类似,但参数以逗号分隔传递

// 给所有的函数添加一个mycall的方法
Function.prototype.mycall = function (context, ...args) {// 在这里可以去执行调用的那个函数(foo)// 问题: 得可以获取到是哪一个函数执行了mycall// 1.获取需要被执行的函数if (typeof this !== "function") {throw new TypeError("Function.prototype.mycall called on non-function");}// 2.对context转成对象类型(防止它传入的是非对象类型)context =context !== null && context !== undefined ? Object(context) : window;// 3.创建唯一属性键const fnKey = Symbol("fn");// 4. 将当前函数绑定到 contextcontext[fnKey] = this;// 5.调用需要被执行的函数var result = context[fnKey](...args);// 6. 删除临时属性delete context[fnKey];// 7.将最终的结果返回出去return result;
};
  • 测试代码
function foo(a, b, c) {console.log(this, a, b, c);
}
foo.mycall({ name: "zs" }, 1, 2, 3);
foo.mycall(null, 1, 2, 3);
foo.mycall(undefined, 1, 2, 3);
foo.mycall(123, 1, 2, 3);

在这里插入图片描述

apply 的实现

立即执行函数,将 this 绑定到指定对象,参数以数组形式传递

// 给所有的函数添加一个myapply的方法
Function.prototype.myapply = function(context, argArray) {// 在这里可以去执行调用的那个函数(foo)if (typeof this !== 'function') {throw new TypeError('Function.prototype.mycall called on non-function');}// 2.对context转成对象类型(防止它传入的是非对象类型)context = (context !== null && context !== undefined) ? Object(context): windowconst fnKey  = Symbol('fn')//  将当前函数绑定到 contextcontext[fnKey] = this// 处理参数,当不传入参数,argArray为undefined,...undefined会报错,所以需要处理一下argArray = argArray || []// 3.调用需要被执行的函数var result = context[fnKey](...argArray)delete context[fnKey]// 4.将最终的结果返回出去return result
}
  • 测试代码
function foo(a, b, c) {console.log(this, a, b, c); 
}
foo.myapply({ name: "zs" }, [1, 2, 3]);
bind 的实现

返回新函数,永久绑定 this 和预设参数

Function.prototype.mybind = function(context, ...argArray) {// 1.获取到真实需要调用的函数if (typeof this !== "function") throw new TypeError("必须是函数");const fn = this// 2.绑定thiscontext = (context !== null && context !== undefined) ? Object(context): windowconst fnKey  = Symbol('fn')function newFn(...args) {// 3.将函数放到context中进行调用context[fnKey] = fn// 特殊: 对两个传入的参数进行合并var finalArgs = [...argArray, ...args]var result = context[fnKey](...finalArgs)delete context[fnKey]// 4.返回结果return result}return newFn
}
  • 测试代码
function foo() {console.log("foo被执行", this)return 20
}function sum(num1, num2, num3, num4) {console.log(num1, num2, num3, num4)console.log(this)
}

在这里插入图片描述

http://www.xdnf.cn/news/1009225.html

相关文章:

  • cf1742D
  • <论文>自注意力序列推荐模型SASRec
  • 负氧离子监测站在景区的作用
  • 详解HarmonyOS NEXT系统中ArkTS和仓颉的混合开发
  • sqlmap 的基本用法
  • 树莓派-ubuntu 24.04开启桌面远程访问
  • MD从入门到荒废-Markdown文件插入多个动态徽章
  • linux驱动开发(6)-内核虚拟空间管理
  • python 在基因研究中的应用,博德研究所:基因编辑
  • JDK各个版本新特性
  • 指针01 day13
  • Python 基础语法 (2)【适合 0 基础】
  • SM4 与 AES 在 GPU 上的性能比较
  • 一分钟了解MCP
  • AES加密
  • Huggingface Transformer 使用指南2-开发自定义模型
  • apdl细节
  • TypeReference指定反序列化获取响应对象
  • 小黑享受思考心流躲避迷茫:92. 反转链表 II
  • 2025年度重点专项项目申报指南的通知公布!
  • ADC(模数转换)
  • 【大模型应用开发】基于langchain的大模型调用及简单RAG应用构建
  • 使用argocd部署nginx
  • 蓝牙协议LMP(Link Manager Protocol)
  • 试验台铁地板:革新工业领域的里程碑
  • java 高并发设计
  • Vue3 + TypeScript + Element Plus 通过 try...catch 捕获不到异常的案例分析
  • P4 QT项目----会学串口助手(解析笔记)
  • 航电系统之数据通信技术篇
  • 管理员端用户操作手册