0
let greetings = {
    fullName : "elham zeinodini",
    getFullName : () => {
        return this.fullName;
    },
    Greet : message => console.log(`${message} ${this.getFullName()} !!`)
}



console.log(greetings.fullName);

greetings.Greet("Hello");
Bravo
  • 6,022
  • 1
  • 10
  • 15
eli
  • 25
  • 1
  • 5
  • first, you must understand the consequences of using an arrow function in there – Bravo Jul 10 '21 at 07:23
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) - The title should give a short summary of the actual problem. It is not meant to contain the whole "question". And please refrain from adding _"Thanks"_ in the question (or the title). If you get an answer that helps "thank" the author with an up-vote and accept the answer that solved your problem. – Andreas Jul 10 '21 at 07:35

3 Answers3

-1

It means, quite literally, that the property getFullName on the context this is not a function. You're trying to invoke something that isn't a function.

Why is that? Because you can't use this like you are to refer to the surrounding object using an arrow function. Depending on your environment, it might refer to the window object (in the browser). And the window object doesn't have a function called getFullName. So that property evaluates to undefined. Which isn't a function, hence the error.

Use a "regular" function declaration instead. getFullName: function() {...}

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
-1

let greetings = {
    fullName : "elham zeinodini",
    getFullName() {
        return this.fullName;
    },
    Greet(message) {
      console.log(`${message} ${this.getFullName()} !!`)
    }
}
console.log(greetings.fullName);
greetings.Greet("Hello");
Bravo
  • 6,022
  • 1
  • 10
  • 15
-2

The issue is with your arrow functions, which don't allow you to capture the this context.

Rewrite as follows:

let greetings = {
    fullName : "elham zeinodini",
    getFullName() {
        return this.fullName;
    },
    Greet(message){ 
        console.log(`${message} ${this.getFullName()} !!`)
    }
}



console.log(greetings.fullName);

greetings.Greet("Hello");
Ace
  • 1,028
  • 2
  • 10
  • 22