结论先行:
call、apply 和 bind 都可以改变 this 的指向;
区别在于:call 和 apply 会立即执行函数,而 bind 不会执行函数,而是返回一个改变了 this 指向的新的函数。其次,在入参格式也有区别,bind 和 call 传入多个参数列表,用逗号分隔。而 apply 是传入一个数组。
它们的实现逻辑/原理基本上是一致的,就是将待执行的函数作为上下文对象的属性,巧妙的改变了 this 的指向,并且立即指向该函数。区别在于,bind 会创建一个函数返回,并且将指定的参数在调用时作为参数传入。
一、bind、call、apply 的区别
① 相同点
都可以改变 this 的指向,传入的第一个参数都是绑定 this 的指向。
本质都是为了改变函数运行时的执行上下文,使得函数在执行过程中的 this 值得到绑定并且传入指定的参数。
这样可以在特定的环境中调用函数,扩展函数的灵活性和适用性。
在非严格模式中,如果第一个参数是 nul 或者 undefined,会把全局对象 window 作为 this 的值。
在严格模式中,null 就是 null,undefined 就是 undefined
② 不同点
1)传入参数格式的区别
bind 和 call 传入的是参数是一个个传入的,用逗号分隔;apply 传入的是数组,也可以是类数组
2)执行时机的区别
call 和 apply 会立即调用,而 bind 返回的是一个改变了 this 指向的函数,你必须调用它才会被执行;
二、实现逻辑、步骤
1、call 函数的实现步骤
- 判断调用对象是否为函数;
- 判断传入上下文对象是否存在,如果不存在,则设置为 window;
- 处理传入的参数,截取第一个参数后的所有参数;
- 将函数作为上下文对象的一个属性;
- 使用上下文对象来调用这个方法,并保存返回结果;
- 删除刚才新增的属性;
- 返回结果;
Function.prototype.myCall = function(context) {
// 判断调用对象是否为函数
if (typeof this !== "function") {
console.error("type error")
return
}
// 判断 context 是否传入,如果未传入则设置为 window
context = context || window;
// 获取参数
let args = [...arguments].slice(1), result = null;
// 将调用函数设为对象的方法
context.fn = this;
// 调用方法
result = context.fn(...args);
// 将属性删除
delete context.fn;
return result;
}
执行结果:
var obj1 = {
a: 1,
getValue: function() {
console.log('obj1', arguments)
return this.a
}
}
var obj2 = {
a: 2,
getValue: function() {
console.log('obj2', arguments)
return this.a
}
}
console.log('call原版', obj1.getValue.call(obj2, 1, 2, 3))
console.log('call模拟板', obj1.getValue.myCall(obj2, 1, 2, 3))
2、 apply 函数的实现步骤
- 判断调用对象是否为函数;
- 判断传入上下文对象是否存在,如果不存在,则设置为 window;
- 将函数作为上下文对象的一个属性;
- 判断参数值是否传入;
- 使用上下文对象来调用这个方法,并保存返回结果;
- 删除刚才新增的属性;
- 返回结果;
Function.prototype.myApply = function(context) {
// 判断调用对象是否为函数
if (typeof this !== 'function') {
console.error('type error')
return
}
// 判断 context 是否传入,如果未传入则设置为 window
context = context || window
// 将待执行的函数作为上下文对象的一个属性
context.fn = this
// 调用方法
let args = arguments[1]
let result = ''
if (args) {
result = context.fn(...args)
} else {
result = context.fn()
}
// 将属性删除
delete context.fn;
return result
}
执行结果:
var obj1 = {
a: 1,
getValue: function() {
console.log('obj1', arguments)
return this.a
}
}
var obj2 = {
a: 2,
getValue: function() {
console.log('obj2', arguments)
return this.a
}
}
console.log('apply原版', obj1.getValue.apply(obj2, [1, 2, 3]))
console.log('apply模拟板', obj1.getValue.myApply(obj2, [1, 2, 3]))
console.log('apply模拟板', obj1.getValue.myApply(obj2))
3、 bind 函数的实现步骤
① 逻辑1
- 判断调用对象是否为函数;
- 判断传入上下文对象是否存在,如果不存在,则设置为 window;
- 将函数作为上下文对象的一个属性;
- 处理传入的参数,截取第一个参数后的所有参数;
- 创建一个函数返回;
- 在函数内部,使用上下文对象来调用这个方法,并返回结果;
- 删除刚才新增的属性;
- 返回结果;
Function.prototype.myBind = function(context) {
// 判断调用对象是否为函数
if (typeof this !== 'function') {
console.error('type error')
return
}
// 判断 context 是否传入,如果未传入则设置为 window
context = context || window
// 将待执行的函数作为上下文对象的一个属性
context.fn = this
// 构造参数
const args = [...arguments].slice(1)
return function() {
// 调用方法
const result = context.fn(...args)
delete context.fn
return result
}
}
② 逻辑2
- 判断调用对象是否为函数;
- 保存当前函数的引用,获取其余传入参数值;
- 创建一个函数返回;
- 函数内部使用 apply 来绑定函数调用,需要判断函数作为构造函数的情况,这个时候需要传入当前函数的 this 给 apply 调用,其余情况都传入指定的上下文对象。
Function.prototype.myBind = function(context) {
// 判断调用对象是否为函数
if (typeof this !== 'function') {
console.error('type error')
return
}
// 构造参数
const args = [...arguments].slice(1)
// 保存当前函数的引用
let fn = this
return function Fn() {
// 调用方法
return fn.apply(this instanceof Fn ? this : context, args.concat(...arguments))
}
}
Function.prototype.myBind = function(context, ...args) {
let fn = this
return function Fn() {
// 调用方法
return fn.apply(context, args)
}
}
执行结果:
var obj1 = {
a: 1,
getValue: function() {
console.log('obj1', arguments)
return this.a
}
}
var obj2 = {
a: 2,
getValue: function() {
console.log('obj2', arguments)
return this.a
}
}
console.log('bind原版', obj1.getValue.bind(obj2, 1, 2, 3)())
console.log('bind模拟板', obj1.getValue.myBind(obj2, 1, 2, 3)())