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

Dennis de Vries
Dennis de Vries
9,440 Points

Understanding 'this': stuck at code challenge

Hey there,

I'm currently stuck on the challenge right after the video 'Understanding this'.

The given code is:

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

And the accompanying question is:

"Modify this object so it uses two properties firstName and lastName and remove their variable declarations from the fullName method. Don't do anything to the console.log() call right now."

The video explaining the working of 'this' in javascript made perfect sense to me. But this question doesn't and I can't figure out what they need me to do. My bet quess would be:

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

But this creates either a syntax error or the error 'contact.firstName should be a string'.

What am I nog seeing here?

Thanks in advance!

4 Answers

Dennis de Vries
Dennis de Vries
9,440 Points

Sigh....

Allright, that was actually a silly question. Anyone interested: it's the friggin' comma right after "Chalkley" and I should've known.

In the very unlikely case someone is as sleepy as I am on this challenge, this is the answer:

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

I'm just going to mark this as 'best answer' and hope noone will ever read this.

OSCAR AU
OSCAR AU
14,008 Points

Thanks, I was stuck. Silly comma...

James Page
James Page
9,621 Points

It would really help if the question made clear that the two properties should also have values!

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

Also need another curly brace at the end

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

I had a weird issue where I couldn't get past the second part of the code challenge. It would error out and tell me the first part of the challenge was no longer passing. When I used this code (which is exactly the same as mine as far as I can tell) it passed. Just in case anyone else runs into that problem.