Sorry if this is a silly mistake, I'm rather new to JavaScript.
So I have a key-function pair ('sayHello') inside the object person
which returns an interpolated string using a template literal (${}). It works fine when I use the standard function syntax.
let person = {
name: 'Ava',
sayHello () {
return `Hello, my name is ${this.name}.`;
}
};
console.log(person.sayHello());
// output: Hello, my name is Ava.
However, when I use arrow function syntax with an arrow token, it returns the 'name' value as undefined.
let person = {
name: 'Ava',
sayHello: () => {
return `Hello, my name is ${this.name}.`;
}
};
console.log(person.sayHello());
// output: Hello, my name is undefined
Does anyone know what the problem is here, even if it is just a syntax error i've made?
Thanks.