Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Help with: "Finally, finish off the code to correctly access the properties on the contact with the this keyword."

I can't even take a stab at this.

var contact = {

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

5 Answers

Erik McClintock
Erik McClintock
45,783 Points

For Task 2, you'll need to use the 'this' keyword to access the properties of your contact object, since they are now properties of the object, rather than being variables declared within the method (which is what they were prior to your edits from Task 1). If you recall, you use the keyword and then either dot or bracket syntax (i.e. this. or this['']) to access the properties of the current object, so you would want to edit your console statement to be:

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

Hope this helps. "This" is a very confusing concept, and can remain to be confusing even long after you've been introduced to it. I definitely recommend rewatching the video a few times, and branching out to other resources to try and learn about it in a variety of ways. You may find one that better clicks with you than Andrew's explanation!

That said, you will be using "this" plenty more throughout this course, so if you don't fully understand it right now, don't fret! Press onward, and with more use cases, it may become clearer.

Erik

not bob
not bob
2,833 Points

I'm not sure if the issue is with 'this', or the way the challenge is phrased. Real confusing.

Andres Ramirez
Andres Ramirez
18,094 Points

Hey Erik!

Off subject.. I was checking your profile, and you have some impressive points, (32,000+), WOW!

I have been playing with HTML and CSS building static responsive websites for about 5 years now, but have not been able to land a job as a developer within a company, yet. I just started the Full Stack Javascript last month, and plan to continue studying full time on Treehouse learning every single coding language I possibly can.

Noticed you said that you landed your first developer job within a few months of learning on Treehouse... Did you have any previous experience? Did you build a portfolio? I would really appreciate some intel on how you managed to get that job so quick!

Good day:) Andy

Erik McClintock
Erik McClintock
45,783 Points

Michael,

Can you give a link to the challenge in question?

Erik

Thank you so much! I tried a few combinations with "this" but none of them worked. It is not an easy concept to grasp and some of the terms used to describe how it works aren't 100% familiar to me yet. I will spend some time trying to grasp this better and press onward! THANKS!

Typically I look at the Workspace that I used to follow along during the lesson and that gives me clues on how to work through tough tasks. But in the lesson, "this" was used in the following form:

var randomNumber = Math.floor(Math.random() * this.sides) + 1;
  console.log(randomNumber);

It doesn't help me figure out how to use the "this" keyword to access the properties in contact..