0

In the normal function i can use this code

const person = {
  name: 'yaman',
  surname: 'katby',

  getFullName: function() {
    return `${this.name} ${this.surname}`
  }
}

What about arrow function by the way the code below not work

const person = {
  name: 'yaman',
  surname: 'katby',

  getFullName: () => `${name} ${surname}`
}

1 Answers1

1

The point of an arrow function is that it doesn't get its own this, it closes over the one where it was created.

In the situation you've shown, you probably wouldn't use an arrow function. But if you did, you'd have to refer to the object by the name of the constant (person), because you can't use this:

const person = {
  name: 'yaman',
  surname: 'katby',

  getFullName: () => `${person.name} ${person.surname}`
};

console.log(person.getFullName());
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639