编程语言
首页 > 编程语言> > javascript – 使用JSON创建对象的实例

javascript – 使用JSON创建对象的实例

作者:互联网

我正在努力学习编程.我已经浏览了一个在线教程网站列表,我认为这对我来说非常重要.

我的问题:

>(这是我最想了解的)在我的for … in循环中,为什​​么使用“Contacts”构造函数创建新的“obj”对象?似乎每次循环时我都需要一个不同的名称,这样我就不会覆盖我之前创建过传递的对象.如果这是正确的,如果我对提前联系的数量或价值一无所知,我该怎么做呢?另外,为什么控制台日志中的任何对象的标题都没有说obj?我是否对创建对象实例意味着什么感到困惑?这些实例的名称是不重要的吗?
>为什么所有属性都未定义?应该引用临时“i”变量的属性吗?

从未知的总数据条目创建对象似乎非常重要.不幸的是,像Codecademy这样的地方在这里不足.您始终使用他们为您提供的硬编码名称手动创建对象的新实例.但如果有两个相同的名字,会发生什么?

非常感谢您对此的任何帮助.不要拒绝告诉我其他任何愚蠢的事情.

以下是控制台屏幕截图的链接 – 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();

解决方法:

In my for…in loop, why is creating new “obj” objects using the “Contacts” constructor working?

变量只是值的保留位置.这不是价值本身.如果用新值覆盖变量,只要某个东西持有对它的引用,它所持有的前一个值将继续存在;它只是变量不再引用它了.

It seems like I would need a different name each time I loop so that I don’t overwrite the object that I’ve created the pass before.

不,你没有.一个变量很好,只要在将变量赋值给下一个值之前对该值执行某些操作即可.

If this is correct, how do I do this if I don’t know anything about the number or value of contacts ahead of time?

你有多少并不重要.

Additionally, why does the title of the any of the objects in the console logs not say obj?

console.log()打印出传递给它的值.它不关心(不知道)关于传递给它的变量的任何信息.

Am I confused about what it means to create an instance of an object?

看来是这样.创建对象的实例会分配一些内存来存储该对象的值.变量允许您访问该分配的对象.

Are the names of these instances unimportant?

是的,对象实例没有名称.

Why are all of the properties undefined?

我保存您正在迭代的对象的属性名称,因此在这种情况下,字符串“contact1”,“contact2”,“contact3”.这些字符串没有name或age属性,因此构造函数接收两个未定义的值.

Should referencing properties from the temporary “i” variable work?

您需要使用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);
}

标签:json,javascript,object,new-operator,addressbook
来源: https://codeday.me/bug/20190609/1206306.html