In this simple program we will create a javascript’s class which can be extended by it’s subclass that will use the super class method to calculate the sum of all numbers within the generator function of the subclass.

First of all let create the main Summation class.

class Summation { constructor() { this._sum_ = 0 // initiate the _sum_ value to 0 } set sum(number) { this._sum_ += number; // perform calculation here } get sum() { return this._sum_; // get the value of the summation result } }

Next we will create the subclass which will then call the function of it’s main class.

class Adding extends Summation { * generator_function() { yield 1000; yield 2000; yield 3000; yield 4000; yield 5000; yield 6000; } // loop through the generator function and adding all the numbers within it calculate_total() { for(let number of this.generator_function()) { super.sum = number; } } print_total() { console.log("The total is " + super.sum); } }

Finally create the new instance of the Adding class then run the calculate_total function and print the total.

let add = new Adding(); add.print_total(add.calculate_total()); // The total is 21000

When we call the method of the super class we will need to use the super keyword as shown above.

## Leave a Reply

Be the First to Comment!