1.改变this指向的的方法
1.caal跟apply跟bind都可以改变this指向
2.call后面是用于一个一个数据数据下使用
3.aply用于后面是数组的情况下
4.bind是修改this指向但是还得调用一下
1.call 用于后面是一个一个数据的情况下
function f(){
console.log(arguments)
Array.prototype.push.call(arguments,100)
}
f(1,5,6)
2.apply用于后面是数组的情况下
function f(){
console.log(arguments)
Array.prototype.push.call(arguments,[1,3,6,9]) //后面是数组的情况下用call的话就不能一个一个的数据放进去了
//使用我们要用 apply
Array.prototype.push.apply(arguments,[1,3,6,9])
aply结果:[1,5,6,1,3,6,9]
call结果:[1,5,6,Array[1,3,6,9]]
f(1,5,6)
3.bind后面要多加需要多加一步()
bind的话后面得从新调用一个否则就是只修改了this指向
function f(){
console.log(arguments)
Array.prototype.pop.bind(arguments)()
}
f(1,5,6)
结果为:
0: 1
1: 5