call() 、 apply() 、bind()方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chen_jia_hao/article/details/81286518

Function.apply(obj,[])方法能接收两个参数:
obj:这个对象将代替Function类里this对象
args:这个是数组,apply方法把这个集合中的元素作为参数传递给被调用的函数。

Function.call(obj,arg1,ar2,arg3,...)方法能接收两个参数:
obj:这个对象将代替Function类里this对象
argi:后面是可变参数,call方法把这些参数作为参数传递给被调用的函数。

Function.bind(obj,arg1,ar2,arg3,...)();方法能接收两个参数:
obj:这个对象将代替Function类里this对象
argi:后面是可变参数,call方法把这些参数作为参数传递给被调用的函数。

它们都可以改变函数体内部 this 的指向。bind是返回对应函数,没有立即调用,apply、call是立即调用。
判断类型:

console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]

猜你喜欢

转载自blog.csdn.net/chen_jia_hao/article/details/81286518