箭头函数
注意:this:
1.在对象的方法中直接使用箭头函数,会指向window,其他箭头函数会指向上一级this,箭头函数并没有存储this
例:axis.get(url).then(function(response){
console.log(this)//undefined
/** 此时 let that=this ,可将this保存到that中 再使用**/
}
adios.get(url).then(response=>{
//此时this指向Vue
}
//对象方法
let obj = {
id:1,
foo:()=>{
return this.id
}
}
let id=2
obj.foo //2
2.通过call、apply是无法绑定箭头函数中的this
例:
let f = () =>this.x
let x=1
f.call({x:2}) //1