当前位置: 首页 > 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)
}

在这里插入图片描述

相关文章:

  • 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 并实现可视化页面
  • 网站建站作业/军事新闻今日最新消息
  • 网站接口怎么做/成都网站关键词排名
  • wordpress基础主题站/网站宣传和推广的方法有哪些
  • 网络公司网站图片/qq引流推广平台
  • wordpress主题几个网站/网络营销案例及分析
  • 网页设计教育培训哪里好/苏州seo关键词优化外包