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

Create a method on the contact object called fullName, use the programming from the printFullName function.

What's wrong...?

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

var contact = {
  var fullName={
printFullName();
};
}

3 Answers

Joel Bardsley
Joel Bardsley
31,246 Points

I would highly recommend going back and watching the previous video again to see how Andrew defined a roll method on his dice object.

Looking at your current code, contact is an object, so properties and methods on objects are formatted like this:

var anObject = {
  propertyName1: "Hello",
  propertyName2: "World",
  methodName: function() {
     console.log(this.propertyName1 + " " + this.propertyName2);
  }
}

As shown above, object properties aren't defined the same way as variables are. While you can't define variables inside an object the way you've done so in your code, you can, however, do so in a method like this:

var anotherObject = {
  methodName: function() {
    var string1 = "Hello";
    var string2 = "World";
    console.log(string1 + " " + string2);
  }
}

Hopefully you can use this as a reference to complete the challenge. If you continue to get stuck or if anything I've mentioned above isn't clear, please let me know.

Mmh I think I get what you're telling me but still stuck with "parse error" on this:

var contact = {

fullName: printFullName(){

  var firstName = "Andrew";

  var lastName = "Chalkley";

  console.log(firstName + " " + lastName);

}}

AH without"printfullname" it works! Thanks again :D