call
先看call做了什么?
- call的第一个参数为函数体内部的this指向
- 其余作为参数单个传入(非数组)函数体
- 立即执行函数
1
2
3
4
5
6
7
8
9
10
11
12Function.prototype.myCall = (context) => {
if(typeof this !== 'function') {
throw new TypeError('Error')
}
context = context || window
context.fn = this
const args = [...arguments].slice(1)
const result = context.fn(...args)
delete context.fn
return result
}apply
与call的区别在于第一个参数后的传参方式, 是以数组的方式传入1
2
3
4
5
6
7
8
9
10
11
12Function.prototype.myApply = (context) => {
if(typeof this !== 'function') {
throw new TypeError('Error')
}
context = context || window
context.fn = this
const args = [...arguments].slice(1)
const result = context.fn(args)
delete context.fn
return result
}
bind 主要注意一点是, 记得区分出new和普通调用的区别
1 | Function.prototype.myBind = (context) => { |