首页 > 移动平台 > 详细

bind、apply、call的实现

时间:2021-03-29 18:12:02      阅读:28      评论:0      收藏:0      [点我收藏+]

1.call的实现

1 Function.prototype.mycall = function (context) {
2   context.fn = this
3   const args = [...arguments].slice(1)
4   const result = context.fn(...args)
5   delete context.fn
6   return result
7 }

2.appyl的实现

1 Function.prototype.myapply = function (context) {
2   context.fn = this
3   const args = arguments[1] ? arguments[1] : []
4   const result = context.fn(...args)
5   delete context.fn
6   return result
7 }

3.bind的实现

1 Function.prototype.mybind = function (context) {
2   const self = this
3   const args1 = [...arguments].slice(1)
4   return function () {
5     const args2 = [...arguments]
6     console.log([...args1, ...args2]);
7     return self.apply(context, [...args1, ...args2])
8   }
9 }

 

bind、apply、call的实现

原文:https://www.cnblogs.com/jianghongyun/p/14592900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!