Function.prototype.myCall = function (context) {/* 1 校验调用call的是否是函数2 设置默认上下文为全局上下文 */if (typeof this !== 'function') {throw new TypeError('is not a function!')}context = context || window || global// step1: 把函数挂到目标对象上(这里的 this 就是我们要改造的的那个函数)context.func = thisconst args = [...arguments].slice(1)// step2: 执行函数const result = context.func(...args)// step3: 删除 step1 中挂到目标对象上的函数,把目标对象"完璧归赵"delete context.funcreturn result}var me = {name: 'zhangsan'}function showName(doing) {console.log(`${this.name} is ${doing}`)}showName.myCall(me,'eating') // zhangsan is eating
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。