从上面四个结果不难看出:
1.call 、bind 、 apply 这三个函数的第一个参数都是 this 的指向对象,第二个参数差别就来了:
2.call的参数是直接放进去的,第二第三第n个参数全都用逗号分隔,直接放到后面 obj.myFun.call(db,'成都', ... ,'string' );
apply的所有参数都必须放在一个数组里面传进去 obj.myFun.apply(db,['成都', ..., 'string' ]);
3.bind除了返回是函数以外,它 的参数和call 一样。
4.call用于参数个数确定的情况,apply用于参数个数不确定的情况
当然,三者的参数不限定是string类型,允许是各种类型,包括函数 、 object 等等!
原文地址:https://www.cnblogs.com/Shd-Study/p/6560808.html
扩展:
function log(){
console.log(arguments);//类数组 包含'hello','+world'的arguments对象
console.log([...arguments])//转化为 包含'hello','+world'的数组
//apply将入参的数组分开,相当于...arguments
console.log.apply(console, arguments);//hello +world
console.log.call(console, ...arguments);//hello +world
};
log('hello','+world')
具体可参考该回答:https://segmentfault.com/q/1010000004452553
关于arguments:
arguments
对象是所有非箭头函数中都可用的局部变量,可以使用arguments
对象在函数中引用函数的参数,该对象包含每个参数可通过索引arguments[index]
获取
arguments
对象是类数组,除了length
属性和索引元素之外,没有任何数组的属性
类数组转化为数组:
let args = Array.prototype.slice.call(arguments)
let args = [].slice.call(arguments)
let args = Array.from(arguments)
let args = [...arguments]