Javascript Course

This lesson shows how to chain object methods in JavaScript.
With the help of method chaining we can call more than one method or function of the object in a single instruction.


To can chain multiple methods, the previous method (method_1() ) must return the object instance (this).

class className {
 //here you can define the properties

 //JavaScript code

 return this; // returns the object instance

 //JavaScript instructions

- Here's an example, a JavaScript class that can calculate the area and perimeter of the rectangle (see the comments in code and test it).
class Rectangle {
 this.a =0;
 this.b =0;

 //sets values for a and b
 setAB(a1, b1){
 this.a = a1;
 this.b = b1;

 return this; // returns the object instance

 // returns area
 return this.a * this.b;

 // returns perimeter
 return 2 * (this.a + this.b);

//creates an object instance of the class
var obR = new Rectangle();

//Set the values to get the area and perimeter
var area = obR.setAB(7, 8).area();
var perimeter = obR.setAB(7, 8).perimeter();

// test
document.write('Area = '+ area +'<br>Perimeter = '+ perimeter);

Chaining multiple methods

You can chain more than two methods, the technique is the same, the public methods (that can be accessed outside) must be defined with the this keyword, and all the previous methods must return the object instance.

- Here is an example that chains three methods. A JavaScript object used to define HTML tag with ID, CSS class, and content (study the code and test it yourself).
class setTag {
 //properties = ''; // id attribute
 this.cls = ''; // class attribute

 // sets id
 setId(id1){ = ' id="'+ id1 +'"';

 return this; // returns the object instance

 // seteaza class attribute
 this.cls =' class="'+ cls1 +'"';

 return this; // returns the object instance

 // returns the HTML and content
 getTagCnt(tag, cnt){
 return '<'+ tag + + this.cls +'>'+ cnt +'</'+ tag+ '>';

//creates an object instance of the class
var obTag = new setTag();

//variables with tag type and content
var tag = 'div';
var cnt = '';

//call methods chained to set ID, "class", and gets a <div> with those attributes
var getTag = obTag.setId('some_id').setClass('a_class').getTagCnt(tag, cnt);

// test
document.write(getTag); //<div id="some_id" class="a_class"></div>
The instruction that chains the methods in the code above:
var getTag = obTag.setId('some_id').setClass('a_class').getTagCnt(tag, cnt);
It is the same with this:
// sets ID and css class, then gets the HTML tag with content
var getTag = obTag.getTagCnt(tag, cnt);

Daily Test with Code Example

Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
 <li><a href="" title="CSS Course">CSS Course</a></li>
 <li><a href="" title="Flash Games">Flash Games</a></li>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Defining classes with Methods that can be chained

Last accessed pages

  1. Get and Modify content of an Iframe (23454)
  2. SHA1 Encrypt data in JavaScript (19325)
  3. PHP PDO - Select query, fetch (24492)
  4. Laravel Ajax (451)
  5. Select in MySQL, Output results in HTML Table (13766)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (494)
  2. Read Excel file data in PHP - PhpExcelReader (228)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (219)
  4. querySelector and querySelectorAll (209)
  5. Create simple Website with PHP (199)