网上有很多讲bind() 函数,自己也在 <你不知道的js> 上卷里面看到了这个方法,根据书上讲的,加上自己理解的,写出一个 bind() 函数供大家参考下
ps: 如有不对的地方,欢迎大家指正!
首先,bind() 函数是一个函数函数调用bind(这里简称fn)之后,返回给你一个新的函数, 在bind() 里面不是直接执行你传入的这个 fn, 而是在bind() 返回给你的函数里面执行这个函数,有点绕,给大家用代码说下:
Function.prototype._bind = function() {
let fn = this; // 因为函数的this 是不会沿着作用域去找的,所有用一个变量接受。
return function() {
fn(); // 执行原函数,
};
};
function a() {};
let newA = a.bind();
newA(); // 这里里面执行a 函数
// 这个是简易的,请大家接着往下看,目的是让大家理解我上面说的那句话
理解了这个之后,我们就开始写完整版的bind()
Function.prototype._bind = function(...args) { // ...args 是es6 里面的rest 参数,它是把所有的参数都放到一个数组。
let fn = this, // 拿到当前函数
bindObj = args.splice(0, 1)[0], // 拿到需要绑定的对象
F = function() {}, // 当传入一个构造函数的时候,用圣杯模式继承原构造函数,圣杯模式大家可以去百度看看
bound; // 返回的函数
bound = function(...oArgs) {
return fn.apply(
this === window ? bindObj : this, // this === window, 说明函数被单独调用,需要改变函数的this指向,让 this = bindObj, 如果函数被 new 调用 || 对对象调用,我们就不需要改变 this指向
oArgs.concat(args)
)
};
// 圣杯模式继承
F.prototype = bindObj;
bound.prototype = new F();
return bound;
};
// 例子1, 单独调用
function add(x, y) {
return x + y;
};
let newAdd = add.bind(null, 1);
let res = newAdd(2);
console.log(res); // 3
// 例子2, new 调用
Father.prototype.firstName = 'j';
function Father(lastName) {
this.lastName = lastName;
};
let Son = Father.bind(null, 'yaya');
let son = new Son(); // 是通过 new 调用的Son, new 的时候,构造函数的 this 是指向它当前的构造函数
console.log(son.lastName, son.firstName); // yaya, j (圣杯模式继承了)