[SOLVED] Dot Notation oojs.html person.greeting() undefined

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 + '.');
    }
  };

No idea why, but person.greeting() is now working when it wasn’t before! shrug

@timandes some kind of strange magic? :wink:

1 Like

Definitely strange magic! From my experience thus far, JS does not fix itself.