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

Explanation Javascript Object

Just a quick explanation please of why the quiz allows one answer to pass? I think this is a mistake... I copied + pasted the question below

Quiz Question 1 of 3

Which of these is NOT a valid way to access a property, "name", on an object?   
//this was the question

 Well done! That's right cat.key is accessing a property called "key".

A   var key = "name"; cat[key];
B   cat["name"];
C   var key = "name"; cat.key;   //this was the answer
D   cat.name;

3 Answers

In the answer C, you gave a value to a var key, and that value was a string "name".

When you are accessing cat.key, it equals to cat."name", which doesn't match the syntax object.key; or object["key"];

So you can't access the property.

If you are using a var key="name" and want to access the property "name", you can only go for cat[key]; , which equals to cat["name"]; .

I was confused about it before because I didn't realize that when I use var key, I was use a string. Hope it helps.

There are only 2 ways to access an object property :

  • object.property
  • object["property"]

Answer C = object."property", this is NOT a valid way to access a property.

Therefore C is wrong ? Am I right?

C is the correct answer because it's only invalid way to access the "name" property of cat.

How can we make C right?

Tommy, by making it into answer A because answer C calls an invalid variable name.

:thumbsup: