You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
587 B
27 lines
587 B
/** |
|
* @description 模拟 bind |
|
* @date 2022-06-30 |
|
* @article https://zhuanlan.zhihu.com/p/50539121 |
|
*/ |
|
|
|
(Function as any).prototype.myBind = function myBind(newThis, ...args) { |
|
// 下边会用到该作用域的 this,所以这里需要保存 this 的指向 |
|
const self = this; |
|
const bindFunction = function (...argss) { |
|
return self.apply(newThis, [...args, ...argss]); |
|
}; |
|
|
|
return bindFunction; |
|
}; |
|
|
|
const obj = { |
|
name: "myBind", |
|
}; |
|
|
|
function original(a, b) { |
|
console.log(this.name); |
|
console.log([a, b]); |
|
} |
|
|
|
const bound = (original as any).myBind(obj, 1); |
|
bound(2);
|
|
|