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

jakefish
jakefish
7,961 Points

Why is this getting an error

I think I followed the instructions perfectly, it didn't seem that complicated, but its getting an error. I tried moving the console.log outside of the object, but that gave me a different error that it could not find firstName

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

2 Answers

Billy Bellchambers
Billy Bellchambers
21,689 Points

These code challenges can be a bit fussy with there required answers sometimes.

This question was only asking you to remove the variables from the function, it didn't ask you to remove the function.

The code it was after should look something like this.

var contact = {
  fullName: function() {
    console.log(firstName + " " + lastName);
  },
  firstName: "Andrew",
  lastName: "Chalkley",
}
Billy Bellchambers
Billy Bellchambers
21,689 Points

also note with your code its not technically a valid object at present which is why it was failing.

Items in an object must all be seperated by a comma. Your final item is ended with a semi-colon.

var contact = { 
firstName: "Andrew", 
lastName: "Chalkley", 
console.log(firstName + " " + lastName);  //note ";" here
}

The name of the challenge is "Understanding this". So it's a good guess that the answer will include the keyword.

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

Just as in the dice example in the video, you pull the variables out (comma separated), and then use this.firstName and this.lastName to use them in the function.