10/30 can't we use dot notation?


#1

https://www.codecademy.com/courses/objects-ii/1/5?curriculum_id=506324b3a7dffd00020bf661#

yes i know that this is the correct code..

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
for(var xyz in nyc)
console.log(nyc[xyz]);

but why is this code wrong(dot notation) ..

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
for(var xyz in nyc)
console.log(nyc.xyz);

#2

Dot notation can only be used for static variables, not dynamic. The key variable in a for..in statement is dynamic, so to access the member it refers to, we must use subscript (array) notation.


#3

Edit- you meant xyz?
so there is no way this program can be written with the dot notation?


#4

xyz is the key variable in the for..in statement, as in,

    var object = {};

    for (var key in object) {
        //
    }

The property names are `keys`, which associate with their respective `values`. We may create and define a new member with dot notation, because it is not dynamic in nature:

    object.newMember = "new property definition";

    console.log( object.newMember ); // new member definition

When querying a property, the key must exist. The above object has no property name defined for `key` or in your case, for `xyz`. Consider,

    xyz = 'newMember';

    console.log( object.xyz );  // undefined

    console.log( object[xyz] ); // new property definition

#5

thank you very much.