Function.prototype.myBind = function () {
// 返回一个绑定this的函数,我们需要在此保存this
let thatFn = this;
if (typeof thatFn !== "function") {
throw new TypeError("what is trying to be bound is not callable");
}
let slice = Array.prototype.slice;
let thatArg = arguments[0],
args = slice.call(arguments, 1);
return function () {
//同样因为支持柯里化形式传参我们需要再次获取存储参数
let newArgs = slice.call(arguments);
return thatFn.apply(thatArg, args.concat(newArgs));
};
};
let person = {
name: "Jack",
};
function say(age) {
return {
name: this.name,
age,
};
}
let say1 = say.myBind(person);
console.log(say1(24));
上一篇
JavaScript 手写 apply 方法.md
// ES5 实现
Function.prototype.myApply1 = function (ctx, args) {
// 不指定this或者传入null或者undifined时,this指向window
ctx = ctx
2020-11-13
下一篇
JavaScript 手写 call 方法.md
// ES5 实现
Function.prototype.myCall = function (ctx) {
// ctx = ctx || window;
ctx = ctx || globalThis; // node环境
2020-11-13