SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reset variable to restart the script.

    Hi.
    Is there a better way to do a thing like this:

    PHP Code:
    var mytest= {
        
    steps:0,
        
    prop1'',
        
    prop2: [],
        
    init:function(){
            if(
    mytest.steps != 0){
                
    // Restart
                // reset variables
                
    mytest.steps0;
                
    mytest.prop1= [];
                
    mytest.prop2= [];
            }
            
    alert(mytest.steps);
            
    alert(mytest.prop1);
            
    alert(mytest.prop2);
        },
        
    count:function(){
            
    mytest.steps++;
            
    mytest.prop1 += Math.random();
            
    mytest.prop2.push(Math.random());
            if(
    mytest.steps 3){
                
    mytest.init();
            }
        }
    }
    mytest.init();
    mytest.count();
    mytest.count();
    mytest.count();
    mytest.count(); 
    I don't like very much reset variables in this way:

    PHP Code:
    if(mytest.steps != 0){
                
    // Restart
                // reset variables
                
    mytest.steps0;
                
    mytest.prop1= [];
                
    mytest.prop2= [];
            } 
    Bye.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this has been discussed recently. Setting it to null was mentioned although that it self could cause problems (http://www.sitepoint.com/forums/showthread.php?t=577003)

    I've personally never been faced with the issue "yet" so i can only point you in the direction of another thread.


  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    this has been discussed recently. Setting it to null was mentioned although that it self could cause problems (http://www.sitepoint.com/forums/showthread.php?t=577003)

    I've personally never been faced with the issue "yet" so i can only point you in the direction of another thread.
    Thanks for the reply.

    My thread wasn't for the reset variables
    like unset in PHP (I've personally never been faced with the issue "yet" )


    I'm wondering if this could be a better way:

    PHP Code:
    var mytest= {
        
    init:function(){
            
    mytest.steps0;
            
    mytest.prop1= [];
            
    mytest.prop2= [];
            
    alert(mytest.steps);
            
    alert(mytest.prop1);
            
    alert(mytest.prop2);
        },
        
    count:function(){
            
    mytest.steps++;
            
    mytest.prop1 += Math.random();
            
    mytest.prop2.push(Math.random());
            if(
    mytest.steps 3){
                
    alert('Restart');
                
    mytest.init();
            }
        }
    }
    mytest.init();
    mytest.count();
    mytest.count();
    mytest.count();
    mytest.count(); 
    I don't like instancing variables inside the 'construct'.

    What do you think about ?
    Last edited by whisher; Oct 15, 2008 at 04:33. Reason: grammar ;)

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think having a separate reset function would be better. As in some cases your init function will only be used when you first start.



Bookmarks

Posting Permissions

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