OBJECTS The this Keyword I undefined problem


#1

I need help with this specific area in my code , its loging this is my name undefined, i dont get why, what am i doing wrong? the rest of the code i understand

sayHello: ()=> {
return this is my name ${this.name}
},

https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/this?action=lesson_resume&course_redirect=introduction-to-javascript
let person = {
name: ‘Tyron’,
age: 40,
weekendAlarm: ‘No alarms needed’,
weekAlarm: ‘Alarm set to 7AM’,

sayHello: ()=> {
return this is my name ${this.name}
},

sayGoodbye () {
return ‘Goodbye!’
}
};

console.log(person.sayHello());

person.hobbies = [‘Basketball’, ‘Coaching’];
person.hobbies = [‘Basketball’];
console.log(person.hobbies);

console.log(person[‘name’]);
console.log(person[‘age’]);

let day = ‘Tuesday’;
let alarm;

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

console.log(person[alarm]);


8.15 - The This Keyword! (Yes ... another one!)
#2

The author wants us to see that the above will not work. Arrow functions do not have a this object like ordinary functions and methods do.

sayHello () {
    return `this is my name, ${this.name}`;
}

#3

Thanks man, the arrow function was the issue, i changed it and the code is displaying the name. I appreciate it


#4

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