javascript simple object creation


/* 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976674&siteId=291194637