This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Benecoder
over 9 years

General on 5/30

I have found the solution for 5/30 but I am still kind of confused what I actually did in this exercise. Why does the Compiler know (in the console.log line) that the value of aProperty is connected to the James Object? Thank you for your Help.

Answer 545fe4e78c1ccc4b3b000ddd

14 votes

Permalink

@Benecoder,

var james = {
    job: "programmer",
    married: false
};

An object has one or more properties separated by a comma-, A property consists of a property-key and it’s associated value.

The james object has 2 properties seperated by a comma-, a job property with property-key job and it’s associated string VALUE “programmer” a married property with property-key married and it’s Boolean VALUE false

It’s all about getting access to the VALUE’s

You either use the literal property-key in a so-called DOT-notation: console.log( james.job );

or You use the so-called Bracket -notation console.log( james[“job”] );

The bracket-notation has the advantage that you can also provide the property-key by using a variable to which a string VALUE is assigned containing the property-key “job”. Thus: var aPropertyKey = “job”; console.log( james[aPropertyKey] );

Is this the answer you where looking for ??

points
Submitted by Leon
over 9 years

4 comments

Benecoder over 9 years

Thank you for you’re fast answer I understood the whole thing now.

igrekov over 9 years

Fantastic explanation, it finally clicked. Thank you so much!

Lance over 9 years

Hello, I did it exactly like that:

var aProperty = “job”; console.log(james[aProperty]);

But it is not working. I get an error:

Oops, try again. It looks like james’ job was not logged to the console.

sloththedweebus over 9 years

I feel like I have a tentative understanding of the concept, what’s holding me back is I don’t understand when I would want to use this. Hopefully the need for it will show up in a future exercise! Otherwise, I don’t see myself using it.

Answer 5474febd8c1ccce8c400080d

2 votes

Permalink

@Lance Truong, As you are manipulating with objects under certain circumstances you can shoot your Browser in an inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code Then use the Reset Code button of the course-window, then paste your code back in.

points
Submitted by Leon
over 9 years

1 comments

sloththedweebus over 9 years

I also had this issue and thought my code was just wrong. Thanks again, Leon.