1

function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eye;
}

var MyFather = new Person('John', 'Doe', 22, "blue");

function getName() {
  let name = this.firstName + ' ' + this.lastName;
  return name;
}

MyFather.getName = getName;
//Will Return John Doe
console.log(MyFather.getName());

var getAge = () => {
  let age = this.age;
  return age;
}

MyFather.getAge = getAge;
//will return undefined
console.log(MyFather.getAge())

In the above code the function get-name will return the desired results. But when I use arrorw function to getAge It will return undefined.

I know that keyword this in arrow function is indicating the window object. Is there anyway around to get the age? using this

Barmar
  • 596,455
  • 48
  • 393
  • 495

0 Answers0