bind、apply、call的实现

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的实现

上一篇:vue - elemebt-admin 移动端select远程搜索软键盘无法弹出


下一篇:vue.cli项目封装全局axios,封装请求,封装公共的api和调用请求的全过程