Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Understanding this

I'm constructing an object with properties and a console.log() call. Why isn't it recognizing my property values?

This is my object:

var contact = { firstName: "Andrew", lastName: "Chalkley", fullName: console.log(contact.firstName + " " + contact.lastName) }

It's telling me that it's contact.firstName and contact.lastName are undefined, not objects ...

I also tried this.firstName and this.lastName, still didn't work. Help :(

object.js
var contact = {
  firstName: "Andrew",
  lastName: "Chalkley",
  fullName: console.log(this.firstName + " " + this.lastName)
}

console.log doesn't belong inside of your object. I'm taking a small guess here as I haven't tested it but "this" is typically used to access properties on within a function. I'm not sure if you can directly access the values your setting while creating the object but just in case, try fullName: contact.firstName + ' ' + contact.lastName; See if that helps.

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points
  fullName: function() {
    console.log(this.firstName + " " + this.lastName)
  }

Is what you are looking for, you have removed the function for some reason :)

Thank you! I thought I did try that -- was probably getting frustrated and stopped, haha. Thanks a bunch!