...

View Full Version : Using a created Object



syco__
07-17-2011, 03:54 AM
Another example from Sams Teach Your self JavaScript.

I have created the new object it prints the business cards out that are stored as you can see here.


function PrintCard() {
line1 = "<b>Name: </b>" + this.name + "<br>\n";
line2 = "<b>Address: </b>" + this.address + "<br>\n";
line3 = "<b>Work Phone: </b>" + this.workplace + "<br>\n";
line4 = "<b>Home Phone: </b>" + this.homephone + "<hr>\n";
document.write(line1,line2,line3,line4);
}

function Card(name,address,work,home) {
this.name = name;
this.address = address;
this.workplace = work;
this.homephone = home;
this.PrintCard = PrintCard;
}

// Create the objects.
sue = new Card ("Sue Suthers", "123 Elm Street", "555-1234", "555-9876");
chris = new Card ("Chris Suthers", "567 Rum Road", "555-4990", "555-8127");
james = new Card ("James Suthers", "17 Junk Court", "555-3334", "555-1596");
ken = new Card ("Ken Suthers", "1 Frank Circuit", "555-2365", "555-23659");

This is all good and i understand it. It is this the printing part i want to know if there is another way to print all 4 of them by naming them in a different way and looping through to print them i think i just answered my own question but it there a built in way to print all of the different cards? here is the current code.


//print Cards
sue.PrintCard();
chris.PrintCard();
james.PrintCard();
ken.PrintCard();

Thanks

devnull69
07-17-2011, 08:51 AM
If you want to loop through the objects of a class you should add those objects to an array


var myObjects = [];
// Create the objects and add them to the array.
sue = new Card ("Sue Suthers", "123 Elm Street", "555-1234", "555-9876");
chris = new Card ("Chris Suthers", "567 Rum Road", "555-4990", "555-8127");
james = new Card ("James Suthers", "17 Junk Court", "555-3334", "555-1596");
ken = new Card ("Ken Suthers", "1 Frank Circuit", "555-2365", "555-23659");
myObjects.push(sue);
myObjects.push(chris);
myObjects.push(james);
myObjects.push(ken);
// loop through the objects
for(i=0; i<myObjects.length; i++) {
myObjects[i].PrintCard();
}

rnd me
07-17-2011, 10:51 AM
element.innerHTML

thesam101
07-17-2011, 11:03 AM
If you want to loop through the objects of a class you should add those objects to an array


var myObjects = [];
// Create the objects and add them to the array.
sue = new Card ("Sue Suthers", "123 Elm Street", "555-1234", "555-9876");
chris = new Card ("Chris Suthers", "567 Rum Road", "555-4990", "555-8127");
james = new Card ("James Suthers", "17 Junk Court", "555-3334", "555-1596");
ken = new Card ("Ken Suthers", "1 Frank Circuit", "555-2365", "555-23659");
myObjects.push(sue);
myObjects.push(chris);
myObjects.push(james);
myObjects.push(ken);
// loop through the objects
for(i=0; i<myObjects.length; i++) {
myObjects[i].PrintCard();
}


Or, if you don't need the card variable names:


<script type="text/javascript">
var myObjects = [];
// Create the objects and add them to the array.
myObjects[0] = new Card ("Sue Suthers", "123 Elm Street", "555-1234", "555-9876");
myObjects[1] = new Card ("Chris Suthers", "567 Rum Road", "555-4990", "555-8127");
myObjects[2] = new Card ("James Suthers", "17 Junk Court", "555-3334", "555-1596");
myObjects[3] = new Card ("Ken Suthers", "1 Frank Circuit", "555-2365", "555-23659");
// loop through the objects
for(i=0; i<myObjects.length; i++) {
myObjects[i].PrintCard();
}
</script>

Should work fine :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum