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 Adding a Method to an Object

Steve Fiedler
Steve Fiedler
5,568 Points

I'm really confused here. I Keep getting the following error "Expected the 'printFullName' function.

Hi Guys,

I'm really confused here. I Keep getting the following error "Expected the 'printFullName' function. To fix this I've tried the following.

  • leaving the printFullName function in the top
  • adding printFullName after the fullName key like this. "fullName: function printFullName(){..."
  • can also calling the function from the object.

Any help on this would be amazing, I really dont know what I've done wrong!

object.js
function printFullName() {
  var firstName = "Andrew";
  var lastName = "Chalkley";
  console.log(firstName + " " + lastName);
}

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

1 Answer

Alex Gervais
Alex Gervais
5,290 Points
function printFullName() {
  var firstName = "Andrew";
  var lastName = "Chalkley";
  console.log(firstName + " " + lastName);
}

var contact = {
  fullName: function(){
     var firstName = "Andrew";
     var lastName = "Chalkley";
     console.log(firstName + " " + lastName);
  }
};
Steve Fiedler
Steve Fiedler
5,568 Points

thanks for that. It looks like I went a bit too far.

Alex Gervais
Alex Gervais
5,290 Points

No problem :) It has something to do with the way objects behave in JavaScript. If you wanted to do it with properties, in this case you'd use the 'this' keyword instead of the object name when referencing the properties. So 'contact.firstName' would become 'this.firstName.'