www.webdeveloper.com
Results 1 to 14 of 14

Thread: trying to run a constructor function via loop, getting error

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    trying to run a constructor function via loop, getting error

    I'm just now learning about, and experimenting with JS's OOP capabilities.

    in the code below i'm trying to run a constructor function inside a loop but am getting the following error via Firebug
    I've put in the error in comments where it occurs. how can i fix this?,

    thanks everyone, have a great weeked.

    PHP Code:
    function cat(name) {
        
    this.name name;
        
    this.talk = function() {
            
    alert(this.name " say meeow!")
            }
    }

    /* THESE WORK PROPERLY
    cat1 = new cat("felix")
    cat1.talk()
    */
    var manyCats = ['George''Tom''Josh''Remy''Jeff']
    for(
    i=0;i<manyCats.length;i++) {
        
    cat+= new cat(manyCats[i]); //ERROR INVALID ASSIGNMENT LEF-THAND SIDE
        //should create cat0 = new cat("George");


  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    var manyCats = ['George', 'Tom', 'Josh', 'Remy', 'Jeff'],
    pussy = [];
    for(i=0;i<manyCats.length;i++) { 
        pussy[i] = new cat(manyCats[i]);
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by toptomato View Post
    cat+i = new cat(manyCats[i]);
    What this statement is saying, is take the function cat, add the value of 'i' to it (not possible), then make it equal an object (of which that same function creates)... I think you can see why it didn't work that way. That's why it was changed to a "pussy" array,,, lmao.. I got a laugh out of this tho, thanks.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    <wink>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Mar 2010
    Posts
    281
    lol thanks,

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Or this code to define global vars which are window properties.
    Code:
    var manyCats = ['George', 'Tom', 'Josh', 'Remy', 'Jeff'];
    for(i=0;i<manyCats.length;i++) {
        window["cat"+i] = new cat(manyCats[i]); 
    }
    alert(cat0.name);

  7. #7
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by 007Julien View Post
    Or this code to define global vars which are window properties.
    Code:
    var manyCats = ['George', 'Tom', 'Josh', 'Remy', 'Jeff'];
    for(i=0;i<manyCats.length;i++) {
        window["cat"+i] = new cat(manyCats[i]); 
    }
    alert(cat0.name);
    Holy crap I just learned something new... Nice one

  8. #8
    Join Date
    Jun 2012
    Posts
    3
    Sounds great!


    ----------------------------------------------
    Rc Helicopter|rc hobby stores|rc helicopters reviews

  9. #9
    Join Date
    Jun 2012
    Posts
    6

    Text is disappearing in text box...

    why does text disappear in the text box when i click the convert button in my temperature converter program...it appears for a sec and the disappears. how can the result be fixed??? thanx...

  10. #10
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    try to name this text box 'pussy' sometimes this works
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  11. #11
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by Padonak View Post
    try to name this text box 'pussy' sometimes this works
    It works even better if you stick something in it after that...

  12. #12
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    ... or move the cursor to-and-fro, clockwise/anti clockwise, faster/slower and finally CLICK-DBLCLICK-TRIPLCLICK roflmao
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  13. #13
    Join Date
    Mar 2010
    Posts
    281
    it's best to create multiple instances of the object pussy

  14. #14
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    yes! and the more the better!
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles