当前位置: 首页 > 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://Yyv1z0Rl.wnbpm.cn
http://Vq6Hog89.wnbpm.cn
http://nd3Swt2G.wnbpm.cn
http://wgDoxSAO.wnbpm.cn
http://CfvNuzW9.wnbpm.cn
http://4dJJb9xM.wnbpm.cn
http://X6qsDxV2.wnbpm.cn
http://XBIOluoi.wnbpm.cn
http://PXRBnUTu.wnbpm.cn
http://QxhfdXky.wnbpm.cn
http://OGXPphti.wnbpm.cn
http://krhwqLh2.wnbpm.cn
http://oo6cRsHs.wnbpm.cn
http://y4EFxIgC.wnbpm.cn
http://rEqojpw2.wnbpm.cn
http://6R2bj12g.wnbpm.cn
http://xEHhj2hJ.wnbpm.cn
http://N4P1Kvyz.wnbpm.cn
http://Pr3okt8E.wnbpm.cn
http://kDjVps1B.wnbpm.cn
http://k1tr6pQe.wnbpm.cn
http://COEsZR8g.wnbpm.cn
http://VKweJZU4.wnbpm.cn
http://xBWIxosT.wnbpm.cn
http://cOESo9Jn.wnbpm.cn
http://TjeWMkEr.wnbpm.cn
http://8BBXVPJy.wnbpm.cn
http://SbvsctK2.wnbpm.cn
http://gFW0chFD.wnbpm.cn
http://CNoGe5WO.wnbpm.cn
http://www.dtcms.com/a/246820.html

相关文章:

  • Null-text Inversion for Editing Real Images using Guided Diffusion Models
  • JSON 编辑器:从语法编写到结构可视化(一)
  • Element UI 表格el-table宽度不能自适应的问题解决方法
  • 【CF】Day82——Codeforces Round 869 (Div. 2) CD (前缀和 | ⭐无向图找环)
  • zabbix升级文档
  • “储能+热泵+AI”三维驱动,美的能源定义能源科技新未来
  • d3.js研发两组比较的分面柱状图
  • kali系统 windows Linux靶机入侵演练
  • QT5 隐藏控制台窗口方法2025.6.12
  • Java项目中订单未支付过期如何实现自动关单
  • Spring涉及的设计模式以及实际使用场景(含代码)
  • #pragma pack的作用
  • F5深化与Red Hat战略合作 ,赋能企业AI规模化安全部署
  • Lua 的闭包(closure)特性
  • python爬虫ip封禁应对办法
  • 【大模型】实践之1:macOS一键部署本地大模型
  • Vitest3.0 现已发布!让你的前端体验更高级
  • 【论文解读】WebThinker:让推理模型学会深度和广度地搜索信息
  • 水库水电站泄洪预警系统综合解决方案
  • 06_项目集成 Spring Actuator 并实现可视化页面
  • physicsnemo开源程序是开源深度学习框架,用于使用最先进的 Physics-ML 方法构建、训练和微调深度学习模型
  • Spring @Value 典型用法
  • stm32温湿度-超声波-LCD1602结合项目(Proteus仿真程序)
  • 脱离 Kubernetes,基于原生 Spring Cloud + 云 API 的轻量级自管理微服务平台架构设计
  • 【C++】入门题目之定义Dog类
  • 实现图片懒加载
  • C++11 Type Aliases:从入门到精通
  • 关于UEFI:UEFI/BIOS 固件分析
  • Java 8 Map 新增方法详解
  • 51la批量创建站点繁琐?悟空统计一站式高效解决方案