在js中call()apply()和bind()都是改变指针指向,他们的功能相同,但用法有区别!
这三个是函数的方法,调用者应为函数!
function.call(obj,ar1,ar2)//第一个参数是函数中此指向的对象,函数在改变此指向的同时还能接收参数以ar1,ar2的格式接收!
function.apply(obj,[ar1,ar2])// apply的用法是多个参数用数组表示
function.bind(obj,ar1,ar2)()// bind和call的区别是后面要加上()调用
a=[{asd:'qwe'}]
a.forEach(function(item){
console.log(this)
}.bind(a))
//改变forEach的指针要用bind 且不加()
应用!
'asd'.forEach(function(item){console.log(item)})
forEach 是数组方法 我们可以用改变this指针的方法让字符串使用
Array.prototype.forEach.call(‘asdasda',function(item,index){console.log(item)})
var bb=[].slice.call('asadsad')
console.log(bb)
返回[a,s,a,d,s,a,d]数组
Math.max.call(null,[123,23,232])