Hello MDN,
I’m working through Object Basics and the interactive oojs.html is giving me some trouble. When testing person.greeting() in dev tools, I’m seeing “Hi! I’m undefined.”
First, we changed name: ['Bob', 'Smith'],
to
name : { first: 'Bob', last: 'Smith' },
… Then we needed to update the methods within the person
object from name[0]
to name.first
, etc.
Why is this.name.first coming back undefined when I try to put in person.greeting()
? Oddly enough, person.bio()
works just fine and I’m not seeing a difference.
const person = {
name : {
first: 'Bob',
last: 'Smith'
},
age: 32,
gender: 'male',
interests: ['music', 'skiing'],
bio: function() {
alert(this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
},
greeting: function() {
alert('Hi! I\'m ' + this.name.first + '.');
}
};