Using JSON to instantiate an object

I am trying to learn how to program. I looked at the list of sites devoted to educational materials on the Internet, and I was stuck with the thing that I think is extremely important for me to understand.

My questions:

  • (This is what I would like to understand most) In my for ... in loop, why is a new obj created using the Contacts constructor? It seems that I would need a different name for each loop so that I do not overwrite the object that I created earlier. If this is correct, how can I do this if I do not know anything about the number or value of contacts ahead of time? Also, why does the title of any of the objects in the console logs not tell obj? Am I confused about what it means to instantiate an object? Are the names of these instances immaterial?

  • Why are all properties undefined? Should references to the properties of the temporary variable "i" work?

Creating objects from an unknown number of data records seems really important. Unfortunately, places like Codecademy aren’t enough here. You always manually create new instances of objects with the hard-coded names that they give you. But what happens if there are two identical names?

Thanks so much for any help I can get from this. Do not hold me back from telling me anything else stupid that I can do.

Here is a link to a screenshot of the console - http://i.imgur.com/TK4dtfV.png

var TestApp = {};

// my data... taken from wherever
TestApp.jsonContacts = {
    contact1: {
        name: "Ethan",
        age: 24
    },
    contact2: {
        name: "Evan",
        age: 30
    },
    contact3: {
        name: "Paul",
        age: 9000
    }
};

// I know this is silly, just let me pretend...
TestApp.jsonStrung = JSON.stringify(TestApp.jsonContacts);

TestApp.globalContactList = [];

// my constructor function to create instances of Contact
TestApp.Contact = function(name, age){
    this.name = name;
    this.age = age;
    TestApp.globalContactList.push(this);
};

// where I'm taking data and creating new Contact objects
TestApp.instantiateObjects = function(){
    // I know this is silly, just let me pretend...
    var jsonUnstrung = JSON.parse(TestApp.jsonStrung);
    // I think I'm looping through the first set of objects sitting in jsonContacts
    for (var i in jsonUnstrung) {
        var obj = new TestApp.Contact(i.name, i.age);
        console.log(obj);
    }
    console.log(TestApp.globalContactList);
};

TestApp.instantiateObjects();
+4
source share
1 answer

for... in loop, "obj" ""?

- . . , , , , - ; , .

, , , .

, . , - , .

, , ?

, .

, obj?

console.log() , . ( ) , .

, ?

, . . .

?

, .

undefined?

i , , "contact1", "contact2", "contact3". name age, undefined.

"i" ?

i :

var obj = new TestApp.Contact(jsonUnstrung[i].name, jsonUnstrung[i].age);



, , TestApp.globalContactList.push(this); . , , :

for (var i in jsonUnstrung) {
    var contact = jsonUnstrung[i];
    var obj = new TestApp.Contact(contact.name, contact.age);
    console.log(obj);
    TestApp.globalContactList.push(obj);
}
+5

Source: https://habr.com/ru/post/1570508/


All Articles