newPromise / js-

0 stars 0 forks source link

箭头函数 this 指向值 #8

Open newPromise opened 6 years ago

newPromise commented 6 years ago

代码如下:

var a = 5;
var person = {
  a : 9,
  geta1: ( ) => {
  console.log(this.a)
},
  geta2: function ( ) {
  console.log(this.a)
}
}

person.geta1() 
// 5
person.geta2() 
// 9
newPromise commented 6 years ago

在箭头函数中. this 指向的是 函数定义生效 的时候定义的对象

函数定义生效: 函数是在哪里被定义的?? 在普通的函数中, this指向的是最后调用的对象