一、箭头函数中的this指向
箭头函数的this指向通常有两种情况
- 如果箭头函数处在一个普通函数之中,那么他的this指向与包裹他的外层函数的this指向一致。
- 其他情况下箭头函数中的this都指向window
let obj = {
fn:function(){
console.log('我是普通函数',this === obj) // true
return ()=>{
console.log('我是箭头函数',this === obj) // true
}
}
}
console.log(obj.fn()())
let obj = {
fn:()=>{
console.log(this === window);
}
}
console.log(obj.fn())
// true
箭头函数特性:this指向规则与限制
本文介绍了箭头函数中this的指向规则,即在普通函数内部时保持外部函数的this,但在其他情况下指向window。同时,强调了箭头函数不能作为构造函数,没有arguments对象,并指出call、apply和bind方法对其this指向无影响。
547

被折叠的 条评论
为什么被折叠?



