Question 5: Why does this work?


#1

This is my code:

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

// set to the first property name of "james"
var aProperty = "job"

// print the value of the first property of "james" 
// using the variable "aProperty"
console.log(james[aProperty]);

This works, but I'm confused by var aProperty = "job" part. How is this able to access the job property of james? I initially expected it to assign the string "job" to aProperty, so I was pretty surprised that it worked. I know that if a variable is declared outside of a function, it is global in scope - does this hold true for properties of that variable / object? Also, if I created another object with the same property key, but different value, would this still work?


#2

@kokkotso,

var friends= {
              bill: { firstName: 'Bill',
                      lastName: 'Gates',
                      number: '444-444-4444',
                      address: [ 'One Microsoft way', 'Redwood', 'WA' ]
              },
             steve: {
                      firstName: 'Steve',
                      lastName: 'Jobs',
                      number: '333-333-3333',
                      address: [ 'Two infinite loop', 'Bentown', 'DV' ] 
              }
   };

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation == only literal property-key allowed

friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation == literal & VARIABLE

1 using the literal property-key

friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]
in this case you will get the string VALUE of the lastName property
of the associated object VALUE of bill


#3

@kokkotso,

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

Description of the james object.
The james object has 3 properties and are seperated by a comma-,

there is a job property with property-key job and it's associated string value 'programmer'
there is a married property with property-key married and it's associated boolean value false
there is a sayJob property with property-key sayJob and it's associated anonymous function VALUE
( they also would 'say', the james-object has the sayJob()-Method )

access via dot-notation
james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE 'programmer'

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

access via the square-bracket-notation
1 using the literal property-key
james["job"]
james["married"]
james["sayJob"]()

2 using the property-key by reference (=== via a variable )
var propertyKey1 = "job";
var propertyKey2 = "married";
var propertyKey3 = "sayJob";
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus
you get the string "programmer"

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= "Hi, I work as a programmer"


#4

@kokkotso

Also, if I created another object with the same property key, but different value, would this still work?

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

    // set to the first property name of "james"
    // var aProperty = "job",  // ignore this and instead
   var aProperty = "surname"; // print out this line 

    // print the value of the first property of "james" 
    // using the variable "aProperty"
    console.log(james[aProperty]);

output display:

jones

When I read what you wrote I was curious to see if I understood what I was learning too so I added in an additional line of code which included surname as the property key and its associated value which was "jones" (to commemorate passing of David Bowie today) and switched the output via the console.log( ) line of code and yes, as expected, it works fine because the aProperty is the name of a variable which stores the property (job or married or surname).

So when the output to the console window displays the result via the line of code:

// print the value of the surname property of "james"
// using the variable "aProperty"
console.log(james[aProperty]);

it is indeed doing as instructed and displaying the associated value of the property key to the console window;

output display:

jones


#5

@awalklearn,
so true.. in memoriam ....

The bracket method to get a the value's of a property-key
is in prepairance, of the usage of
the so-called FOR-IN loop

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#6

@leonhard_wettengmx_n

RIP DB ironic that he died at the age of 69 as I think that was the same year he released space oddity.

Anyway, back to learning....thanks for posting the explanatory notes and elaborating some more on this for me. I will add these notes you provided to the other notes you wrote so it helps me build up my knowledge base as I have an inkling I will be referring back to the notes for quite some time.