javascript构造函数语法

时间:2014-09-15 10:31:10

标签: javascript constructor

我想创建一个用于管理联系人的简单应用程序。我希望能够添加新联系人和删除联系人。因此,我认为最好的方法是创建一个联系人构造函数,以便能够使用它创建联系人对象。我计划在这个构造函数中使用set和get方法。

如何在其中创建包含这些值的联系人构造函数。

联系信息应表示为JSON,例如:

{
  "id": "0001",
  "first_name": "Charles",
  "last_name": "Bronson",
  "year_of_birth": 1921,
  "email": "charles@bronson.com",
  "image_url": "http://image.toutlecine.com/photos/b/r/o/bronson-charles-01-g.jpg",
  "addresses": {
    "address": [
      { "id": "1001", "street_name": "Storgata", "city": "Åhus" },
      { "id": "1003", "street_name": "Lillgata", "city": "Åhus" },
    ]
  },
  "phones": [
    { "id": "5001", "type": "mobile", "number": "070112233" },
    { "id": "5002", "type": "home", "number": "046445566" 
  ]
}

这是创建这种构造函数的正确方法吗?

function contacts() {
  this.id = id;
  this.first_name = first_name;
  this.last_name    = last_name;
  this.year_of_birth = year_of_birth;
  this.email = email;
  this.image_url = image_url;
  this.adressess = adress["id", "street_name", "city"];
  this.phones = phone["id", "type", "number"];
}

谢谢!

1 个答案:

答案 0 :(得分:0)

addresses个对象中有一组地址对象:

addresses.address[0].id是获取该数组中第一个地址对象的id的方法。

addresses.address[1].id将获得第二个地址对象的id

依旧......