简要回答
- 改变函数执行时的this指向
- call/apply:
fun执行的结果
- bind:返回
fun的拷贝,并拥有指定的this值和初始参数
- apply第二个参数为数组,数组内的值为传给
fun的参数
- 调用call/apply/bind的必须是个函数
语法
fun.call(thisArg, param1, param2, ...)
fun.apply(thisArg, [param1,param2,...])
fun.bind(thisArg, param1, param2, ...)
var obj1={
name:"小明",
fn1:function(val1,val2) {
console.log(this.name,val1,val2);
}
}
var obj2={
name:"小红",
fn2:function() {
console.log(this.name);
}
}
console.log(this.obj1.fn1.call(obj2,111,222))
console.log(this.obj1.fn1.apply(obj2,[111,222]))
console.log(this.obj1.fn1.bind(obj2,111,222)())
手写一个call
- 根据call的规则设置上下文对象,也就是
this的指向。
- 通过设置
context的属性,将函数的this指向隐式绑定到context上
- 通过隐式绑定执行函数并传递参数。
- 删除临时属性,返回函数执行结果

Function.prototype.myCall = function (context, ...arr) {
if (context === null || context === undefined) {
context = window
} else {
context = Object(context)
}
const specialPrototype = Symbol('特殊属性Symbol')
context[specialPrototype] = this
let result = contextspecialPrototype
delete context[specialPrototype]
return result
}