How to invoke methods of a particular class in TypeScript?


In this tutorial, users will learn to invoke a particular class method in TypeScript. The class is the basic concept of object-oriented programming. In simple terms, it contains member variables and methods we can access by creating the object of that particular class. So, class is the blueprint of the objects we create for that particular class.

The class can contain the functions in TypeScript, and we can also call them to the method. So, users need to learn to access and invoke the method of the particular class.

Invoking the method of a particular class in TypeScript

In TypeScript, we can create the object instance of a particular class. Users can access the public members and methods of the particular class using the dot operator by taking the object of that class as a reference.

Syntax

Users can follow the syntax below to learn to create the object of the class and access the method of that class using the object and dot operator.

// Creating the class
class Getsum {
   doSum(multiplier: number): number {
	   // perform some operation
      return value;
   }
}
var object: Getsum = new Getsum();
let result = object.doSum(10);

In the above syntax, we have simply created the class containing the method. Also, we have defined the object instance of the class and accessed the doSum() method using the dot operator.

Example

In the example below, we have created the ‘GetSum’ class. We also defined the num1 and num2 of the type number as a class member and initialized them with the 10 and 20 values, respectively.

Next, we have defined the doSum() method, which takes the ‘multiplier’ as a parameter and returns the number value. Inside the method, we are accessing the num1 and num2 using the ‘this’ keyword and multiplying both with the multiplier. After that, we do the sum of the new values of the num1 and num2, store it in the ‘sum’ variable, and return it.

// Creating the class
class Getsum {
   // defining the member variables of the class
   num1: number = 10;
   num2: number = 20;
   //   Method to get sum of two numbers
   doSum(multiplier: number): number {
      // Multiply num1 and num2 by multiplier
      this.num1 = this.num1 * multiplier;
      this.num2 = this.num2 * multiplier;
      // do sum of new num1 and num2, which we got after multiplying with the multiplier
      let sum = this.num1 + this.num2;
      return sum;
   }
}

// Creating the object of the Getsum class
var object: Getsum = new Getsum();
// calling the doSum() method
let result = object.doSum(10);
console.log("Result we get after calling the doSum method is " + result);

On compiling, it will generate the following JavaScript code −

// Creating the class
var Getsum = /** @class */ (function () {
   function Getsum() {
      // defining the member variables of the class
      this.num1 = 10;
      this.num2 = 20;
   }
   // Method to get sum of two numbers
   Getsum.prototype.doSum = function (multiplier) {
      // Multiply num1 and num2 by multiplier
      this.num1 = this.num1 * multiplier;
      this.num2 = this.num2 * multiplier;
      // do sum of new num1 and num2, which we got after multiplying with the multiplier
      var sum = this.num1 + this.num2;
      return sum;
   };
   return Getsum;
}());
// Creating the object of the Getsum class
var object = new Getsum();
// calling the doSum() method
var result = object.doSum(10);
console.log("Result we get after calling the doSum method is " + result);

Output

The above code will produce the following output −

Result we get after calling the doSum method is 300

Invoking the method of one class to another class

In TypeScript, we can access the public method of one class to another class and can invoke it. Sometimes, it requires calling small methods of other classes to a particular class.

Syntax

Users can follow the syntax below to invoke the method of one class to another class.

class Getsum {
   doSum(): number {
      // perform some operation
   }
}

class Multiplier {
   object: Getsum = new Getsum();
   result = this.object.doSum();
   multiply(): number {
      // Perform some operation
   }
}
let multiplierObject: Multiplier = new Multiplier();
let result =  multiplierObject.multiply()

In the above syntax, we have created two different classes. We have called the method of the first class to the second class and used its returned value inside another method of the second class.

Example

In the example below, the GetSum class contains the doSum() method as a member which returns the number value. In the Multiplier class, we created the object of the GetSum class and invoked the doSum() method of GetSum class by taking the object as a reference and storing its returned value in the result.

Also, we created the multiply method inside the Multiplier class, which multiplies the result by 30 and returns the multiplication value. At last, we have created the object of the Multiplier class and invoked the multiply method by taking it as a reference.

// Creating the class
class Getsum {
   // defining the member variables of the class
   num1: number = 145;
   num2: number = 243;
   //   Method to get sum of two numbers
   doSum(): number {
      let sum = this.num1 + this.num2;
      return sum;
   }
}

class Multiplier {
   // Creating the object of the Getsum class
   object: Getsum = new Getsum();
   // calling the doSum() method of the GetSum class inside the Multiplier class
   result = this.object.doSum();
   //  Method to multiply number
   multiply(): number {
      return this.result * 30;
   }
}
// Creating the object of the Multiplier class
let multiplierObject: Multiplier = new Multiplier();
// Invoking the multiply method of multiplier class
console.log(
   "Result we get after calling the multiply method is " +
   multiplierObject.multiply()
);

On compiling, it will generate the following JavaScript code −

// Creating the class
var Getsum = /** @class */ (function () {
   function Getsum() {
      // defining the member variables of the class
      this.num1 = 145;
      this.num2 = 243;
   }
   //   Method to get sum of two numbers
   Getsum.prototype.doSum = function () {
      var sum = this.num1 + this.num2;
      return sum;
   };
   return Getsum;
}());
var Multiplier = /** @class */ (function () {
   function Multiplier() {
      // Creating the object of the Getsum class
      this.object = new Getsum();
      // calling the doSum() method of the GetSum class inside the Multiplier class
      this.result = this.object.doSum();
   }
   //  Method to multiply number
   Multiplier.prototype.multiply = function () {
      return this.result * 30;
   };
   return Multiplier;
}());
// Creating the object of the Multiplier class
var multiplierObject = new Multiplier();
// Invoking the multiply method of multiplier class
console.log("Result we get after calling the multiply method is " +
   multiplierObject.multiply());

Output

The above code will produce the following output −

Result we get after calling the multiply method is 11640

Invoking the Static Method of the particular class in TypeScript

We can access static methods directly without taking the reference of the object of the class in which the method is declared. We need to use the static keyword before the method definition to define the static method.

Syntax

Users can follow the syntax below to define and invoke the static method.

class demoClass {
   static printMessage(): void {
      // perform some operation
   }
}
demoClass.printMessage();

In the above syntax, we define the class containing the static method. Also, we have invoked the static method by taking the class name as a reference.

Example

In the example below, we have created the class name demoClass. We have defined the static method inside the class by inserting the static keyword before the method definition, which prints the message.

At last, we used the demoClass as a reference and invoked the printMessage() method.

class demoClass {
   // add static keyword before method defination to make it static.
   static printMessage(): void {
      console.log(" Static method name printMessage is called.");
   }
}
// call the static method by taking class name as a reference
demoClass.printMessage();

On compiling, it will generate the following JavaScript code −

var demoClass = /** @class */ (function () {
   function demoClass() {
   }
   // add static keyword before method defination to make it static.
   demoClass.printMessage = function () {
      console.log(" Static method name printMessage is called.");
   };
   return demoClass;
}());
// call the static method by taking class name as a reference
demoClass.printMessage();

Output

The above code will produce the following output −

Static method name printMessage is called.

Users learned to invoke the method of the class in TypeScript via this tutorial. Also, they learned to invoke the static method of the particular class. Furthermore, users learned to call one class's method to another, which helps them to reuse the code.

Updated on: 16-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements