The JavaScript Diaries: Part 12
http://www.htmlgoodies.com/primers/jsp/article.php/11915_3672361_2/The-JavaScript-Diaries-Part-12.htm (Back to article)
function guitar(maker,model,color) {
this.maker=maker;
this.model=model;
this.color=color;
}
Let's look at the example above as a refresher on how to create a constructor function:
|
Next, we'll create a new array to contain our data:
var indivModels = new Array();
indivModels[0] = new guitar("Gibson", "Les Paul", "Sunburst");
indivModels[1] = new guitar("Fender", "Stratocaster", "Black");
indivModels[2] = new guitar("Martin", "D-28", "Mahogany");
indivModels[3] = new guitar("Takamine", "EG330SC", "Spruce");
Here we created a variable called indivModels and initialized it with a value of a new instance of the Array object. Then we created an array for each of the elements within the original array. These add another "dimension" to the array, hence the term "multidimensional." Each of the new arrays contains three elements.
Finally, we need to retrieve the information. To obtain the maker of a designated guitar, we can use the following method:
var myAcoustic = indivModels[3].maker;
alert("My guitar is made by" + myAcoustic);
We declared a variable named myAcoustic and initialized it with the value of the maker parameter of the fourth element "[3}" of the array named indivModels. Here's another opportunity to retrieve other data, if you wish:
var myAcoustic = indivModels[3].maker;
var myModel = indivModels[3].model;
alert("My guitar is a " + myModel + " made by " + myAcoustic);