hgq's docs
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)

guoguoqiqi

漫不经心的向往
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)
  • call的实现
  • apply的实现
  • bind的实现
  • new的实现
  • instanceof的实现
  • Object create()的实现
  • Object assign()的实现
  • Promise的实现
  • 实现防抖函数(debounce)
  • 实现节流函数(throttle)
  • 深拷贝(deepclone)
  • 数组扁平化的实现(flat)
  • 函数柯里化
  • EventEmitter 实现
  • 数组的随机排序
  • 通用的事件侦听器函数
  • 实现一个JSONP
  • 数组的map方法
  • 数组的forEach方法
  • 数组的find方法
  • 数组的findIndex方法
  • 数组的some方法
  • 数组的filter方法
  • 数组的every方法
  • 数组的includes方法
  • 数组的fill方法
  • 数组的join方法
  • 数组的flat方法
  • 用setTimeout实现setInterval
  • 用setInterval实现setTimeout
  • 实现一个较完善的深拷贝
  • 代码手写
guoguoqiqi
2022-03-04

bind的实现

Function.prototype.bind2 = function (context) {

    if (typeof this !== "function") {
      throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var self = this;
    var args = Array.prototype.slice.call(arguments, 1);
    var fNOP = function () {};

    var fbound = function () {
        self.apply(this instanceof self ? this : context, args.concat(Array.prototype.slice.call(arguments)));
    }

    fNOP.prototype = this.prototype;
    fbound.prototype = new fNOP();

    return fbound;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 参考文章链接

JavaScript深入之bind的模拟实现 (opens new window)
面试官问:能否模拟实现JS的bind方法 (opens new window)

← apply的实现 new的实现→

最近更新
01
vuex数据持久化怎么做
05-22
02
vue的动态路由怎么配置使用
05-22
03
vue权限控制一般怎么做
05-22
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Guoquoqiqi | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式