Introduction to Javascript - Objects The "this" Keyword I


#1

I am trying to call the method sayHello() but I got the following error below

I have added the ‘this’ keyword so that it could define the variable inside the object. According to the error message this is a syntax error not reference error but I can’t see anything wrong with the syntax. Can you please advise ?

This is my code:

var day = "Monday";
let alarm; 

var person = {
  weekendAlarm: 'No alarms needed', 
  weekAlarm: 'Alarm set to 7AM',
  name: 'james', 
  sayHello: () => {
    return 'Hello, my name is
   ${this.name}' 
  },
  sayGoodbye() {
  return 'Goodbye!'
}
};

person.hobbies =['music', 'martial arts'];
person.hobbies =['music']; 

if (day == 'Saturday'|| day == 'Sunday') {
  alarm = 'weekendAlarm';
} else  {
  alarm = 'weekAlarm';
}

console.log(person[alarm]); 
console.log(person.hobbies);
console.log(person.sayHello());

#2

Please remember to include a link to the exercise when posting a question. Thanks.

Your template string needs to in backtick quotes…

 return `Hello, my name is ${this.name}.`

We only need one of the above lines. The second of the two may be removed.


#4

Thank you. it works now :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.