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

j建网站凡科网怎么建网站

j建网站,凡科网怎么建网站,江苏专业网站建设公司电话,网站优化多少钱JavaScript 中 apply、call 和 bind 方法的手写实现 call 的实现 与 apply 类似,但参数以逗号分隔传递 // 给所有的函数添加一个mycall的方法 Function.prototype.mycall function (context, ...args) {// 在这里可以去执行调用的那个函数(foo)// 问题: 得可以获…
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.dtcms.com/wzjs/75575.html

相关文章:

  • 安州区建设局网站营销推广怎么做
  • wordpress分类栏目关键词山西seo
  • 网站设计的特点网店运营策划方案
  • 盐城网站建设费用自媒体135的网站是多少
  • 网站建设职位网站定制开发
  • 上海房产做哪个网站好百度刷自己网站的关键词
  • 兴义市住房和城乡建设网站月销售百度推广登录官网入口
  • 网站地图怎么添加今日头条新闻消息
  • 一个月做网站百度一下首页百度
  • 网站做跳转链接网站关键词免费优化
  • 做dm素材网站人工智能培训心得
  • 网站程序找人做还是自己做seo优化网站的注意事项
  • 怎么利用网站做外链接中国十大策划公司排名
  • 中山seo推广优化武汉seo排名
  • 网站后台内容编辑器如何优化关键词的方法
  • 什么网站帮人做网页网站点击快速排名
  • 长沙建站网站2022年免费云服务器
  • 帮诈骗公司做网站网站推广seo设置
  • 北京做网站的好公司莆田seo推广公司
  • 爱站网怎么使用百度首页推荐关不掉吗
  • 青岛 网站设计国家再就业免费培训网
  • 班级展示网站百度推广培训
  • 做网站如何接单河南seo快速排名
  • 沈阳企业自助建站系统网络营销推广的
  • 茶叶网站开发目的和意义北京seo外包公司要靠谱的
  • 黑龙江建设网站广东全网推广
  • 网站收录不增加品牌推广方案模板
  • 日本建设网站北京seo优化排名
  • 建设个人网站用什么软件好dw网页制作教程
  • 钓鱼网站实施过程win优化大师怎么样