Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts

    Using a created Object

    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.

    Code:
    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.

    Code:
    //print Cards
    sue.PrintCard();
    chris.PrintCard();
    james.PrintCard();
    ken.PrintCard();
    Thanks
    .pLeAd InSaNiTy.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    If you want to loop through the objects of a class you should add those objects to an array
    Code:
    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();
    }

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    element.innerHTML
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by devnull69 View Post
    If you want to loop through the objects of a class you should add those objects to an array
    Code:
    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:

    Code:
    <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
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •