javascript 简单对象创建


/* array literal */
var aData = [];

/* object constructur */
function Data(firstname, lastname) {
  this.firstname = firstname;
  this.lastname = lastname;
  this.fullname = function() {
    return (this.firstname + " " + this.lastname);
  };
}

/* store object into array */
aData.push(new Data("Jhon", "Doe"));
aData.push(new Data("Anna", "Smith"));
aData.push(new Data("Black", "Pearl"));

/* convert array of object into string json */
var jsonString = JSON.stringify(aData);
document.write(jsonString);

/* loop arrray */
for (var x in aData) {
  alert(aData[x].fullname());
}

https://stackoverflow.com/questions/6254050/how-to-add-an-object-to-an-array

猜你喜欢

转载自my.oschina.net/ITELITE/blog/1802395