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.


Sampled by © JavaScriptBank.com

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();

};

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website
How To Create A Successful Prototype For Your PCB


Top view articles
Top 10 Beautiful Christmas Countdown Timers
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 10 Best JavaScript eBooks that Beginners should Learn
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com