下列代码的执行结果
var a = 1
function fn1() {
console.log(this.a)
}
const fn2 = ()=> {
console.log(this.a)
}
const obj = {
a: 10,
fn1: fn1,
fn2: fn2
}
fn1()
fn2()
obj.fn1()
obj.fn2()
解析:本题考查this指向问题,以及箭头函数中this指向。
箭头函数中的this始终指向其父级作用域中的this。换句话说,箭头函数会捕获其所在的上下文的this值,作为自己的this值。在箭头函数中调用 this 时,仅仅是简单的沿着作用域链向上寻找,找到最近的一个 this 拿来使用,它与调用时的上下文无关。