1. call bind 和 bind 方法的区别和深入理解
1.1. 用法的区别
const me = {
name: 'xiuyan',
age: 18
}
function showName(a, b) {
console.log(`${this[a]}:${this[b]}岁`)
}
showName.call(me, 'name', 'age')
showName.apply(me, ['name', 'age'])
showName.bind(me, 'name', 'age')()
1.2. call的模拟
Function.prototype.myCall = function(context, ...args) {
context.func = this
context.func(...args)
delete context.func
}
1.3. apply的模拟
Function.prototype.myApply = function(context, args) {
context.func = this
context.func(...args)
delete context.func
}
1.4. bind的模拟
Function.prototype.myBind = function(context, ...args) {
context.func = this
return () => {
context.func(...args)
delete context.func
}
}