[TOC]

1、先谈谈call和apply
call和apply是function.proptotype方法,用于改变函数运行的上下文。最终的返回值是你调用的方法
eg:
// obj的myFun方法中的this指向最终指向了db中

// call、apply内部实现 context 在严格模式中,如果传入的值为null, undefined, 默认不会取windows, 在非严格模式下,默认指向windows
Function.prototype.myCall = function(context, …args){
if(context === undefined || context === null){
context = windows;
} else {
context = Object(context);
}
// 获取唯一标识
var fn = Symbol();
context[fn] = this;
let result = contextfn;
delete context[fn];
return result;
}

Function.prototype.myApply = function(context, args){
if(context === undefined || context === null){
context = windows;
} else {
context = Object(context);
}
const fn = Symbol();
context[fn] = this;
let result = contextfn;
delete context[fn];
return result;
}

// bind 函数
// 1、返回原来函数的拷贝(每次返回都是一个新函数)
// 2、将函数中的this固定为bind方法中的第一个参数
// 3、以后无论由哪个对象调用绑定函数,绑定函数中this依然由当时调用的bind方法的一个参数决定
// 4、如果绑定的由构造函数来使用,则this不生效
// 5、调用bind方法的第二个参数为绑定函数的第二个参数,称为预设参数,调用绑定函数时的第一个参数排在预设参数后面
// 6、无论使用任何调用bind方法时不传参数,第一个参数为undefined或null,绑定的this指向window
Function.proptotype.mybind = function(context, innerArgs){
var me = this;
return function(finallyArgs){
return me.apply(context, …innerArgs, …finallyArgs)
}
}