google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank
Guest, register






JavaScript Chaining Methods If you worked on many JavaScript and jQuery projects, you should be familiar with many JavaScript source codes such as Class.method1().method2().methodN(). This JavaScript article tutorial provides some basic concepts about this field. Please go to the inner page for full details.


Label: JavaScript Chaining Methods, JavaScript applications, jQuery projects

Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

JavaScript allows us to chain methods by simply making methods return the this keyword, that is, themselves. So you can have method1().method2().methodN() and so on, all members of the same chain. This turns out to be very useful when you want to concatenate some subroutines and, most of all, when you want to keep your code tidy and organized by avoiding functions and methods that make too much. The basic structure is as follows:

var Class = {
  
  method1: function() {
  
    //...
    
    return this;
  
  },
  
  method2: function() {
  
    //...
  
    return this;
  
  },
  
  methodN: function() {

    // end of the chain

  }
};

You can use this technique as follows:

Class.method1().method2().methodN();

A practical example:

var Class = {

  element: document.getElementById('test'),
  
  grow: function() {
  
  
    this.element.style.width = '180px';
    this.element.style.height = '180px';
    
    
    return this;
  
  
  },
  
  move: function() {
  
  
    var counter = 0;
    
    var interval = setInterval(function() {
    
    
      counter += 1;
      
      Class.element.style.left = counter + 'px';
      
      if(counter == 100) {
      
      
        clearInterval(interval);
        
        var interval2 = setInterval(function() {
        
        
          counter -= 1;
          
          Class.element.style.left = counter + 'px';
          
          if(counter == 0) {
          
            clearInterval(interval2);
            
            Class.element.style.width = '150px';
            Class.element.style.height = '150px';
          
          
          }
        
        
        
        }, 25);      
      
      }
    
    
    
    }, 25);
  
  
  },
  
  run: function() {
  
    var trigger = document.getElementById('run');
    
    run.onclick = function() {
    
      Class.grow().move();
      
      return false;
    
    
    
    };
  
  
  
  }


};


Class.run();

};

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web