实现简单的bind函数.md

模拟传参实现

1
2
3
4
5
6
7
Function.prototype.bind = function (...args) {
const self = this
const bindTarget = args.shift()
return function (...args1) {
return self.apply(bindTarget, [...args, ...args1])
}
}

完整版perf: 加上构造函数作用域实现

1
2
3
4
5
6
7
8
Function.prototype.bind = Function.prototype.bind || function (context, ...args1) {
const self = this
const fBound = function (...args2) {
return self.apply(context, [...args1, ...args2])
}
fBound.prototype = this.prototype
return fBound
}