javascript中的箭头和常规功能表示法有什么区别?

时间:2018-06-19 13:13:01

标签: javascript ecmascript-6

var rahul = {
  name: 'Rahul',
  lastName: 'Kumar',
  yearOfBirth: 1990,
  calculateAge: function() {
    return 2018 - this.yearOfBirth;
  }
}
console.log(rahul.calculateAge());
// output : 31
var rahul = {
  name: 'Rahul',
  lastName: 'Kumar',
  yearOfBirth: 1990,
  calculateAge: () => {
    return 2018 - this.yearOfBirth;
  }
}
console.log(rahul.calculateAge());
// Output : NaN

0 个答案:

没有答案