Static Method in TypeScript

Static Method in TypeScript

 
Whenever you want to access methods of a class (but the method of the class is not static), then it is necessary to create an object of that class. But if the static keyword is used with a member function (method), that method is automatically invoked without creating an object of the class, you just invoke that method using the class name .(dot) method name. In short, if we say about static methods, the static keyword enables us to use methods of a class without instantiating an object first. In static methods, you can define both static and non-static data members, and you can also use this keyword in static methods.
 

Syntax

  1. access modifier static(keyword) MethodName(Parameter(Optional)) {  
  2.   ...........  
The following example tells you, how to use a static method in TypeScript. Use the following procedure to create a program using a static method.
 
Step 1
Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is subsequently opened. Provide the name of your application, like "ExOfstaticMethod", then click on the Ok button.
 
Step 2
After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, CSS file, and HTML files.
 
Step 3
The code of the static method program:
 

ExOfstaticMethod.ts

  1. class StaticMethod {  
  2.  public fname: string;  
  3.  publicstatic lname: string;  
  4.  static MyFunction(fname: string) {  
  5.   document.write("I am a static member function");  
  6.   this.fname = fname;  
  7.   this.lname = "solution";  
  8.   alert("" + this.fname + " " + this.lname);  
  9.  }  
  10. }  
  11. window.onload = () => {  
  12.  StaticMethod.MyFunction("Mcn");  
  13. }  
Note: In the above-declared program I have created a static method with the argument , in this program the static method is called without creating the object of the class.
 

default.html

  1. < !DOCTYPEhtml >  
  2.  <  
  3.  htmllang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  metacharset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  linkrel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  scriptsrc = "app.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h1 > TypeScript HTML App < /h1>  
  23.  <  
  24.  divid = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  

app.js

  1. var StaticMethod = (function() {  
  2.  function StaticMethod() {}  
  3.  StaticMethod.lname = "";  
  4.  StaticMethod.MyFunction = function MyFunction(fname) {  
  5.   document.write("I am a static member function");  
  6.   this.fname = fname;  
  7.   this.lname = "solution";  
  8.   alert("" + this.fname + " " + this.lname);  
  9.  }  
  10.  return StaticMethod;  
  11. })();  
  12. window.onload = function() {  
  13.  StaticMethod.MyFunction("Mcn");  
  14. };  
Output
 
static-method-in-typescript.jpg


Similar Articles