Home > other >  Arrow function undefined
Arrow function undefined

Time:09-25

Does anyone know why the function without the arrow is showing the name value, but if use arrow function is showing the message undefined ?

Without the arrow function:

 const test = {
    prop: 42,
    name: "Leo",
    func: function(){
         return this.name
     },
   };

  console.log(test.func());

With the arrow function:

 const test = {
    prop: 42,
    name: "Leo",
    func: function() => this.name,
   };

  console.log(test.func());

Here is showing the message: undefined

CodePudding user response:

That is not how you declare an arrow function.

Remove the function part:

const test = {
  prop: 42,
  name: "Leo",
  func: () => this.name,
};

console.log(test.func());

  • Related