Difference between TypeScript Class Function, Class Instance Function and Static Function<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Difference between TypeScript Class Function, Class Instance Function and Static Function

Posted on: May 1, 2017

This look like a very basic question to ask. What is the difference between the three different kind of functions you can have in a class? First, some people may be surprise to know that there is more than two. Let's see some code and look at the difference between the three ways to have function.

1class User {
2 private name: string;
3 constructor(initialName: string) {
4 this.name = initialName;
5 }
6 public greet(): string {
7 return "Hello, " + this.name;
8 }
10 public assign = (s: string, x?: string): void => {
11 this.name = s;
12 if (x === undefined) {
13 console.log("Undefined");
14 }
15 };
17 private static count = 0;
18 public static assignStatic() {
19 User.count = User.count + 1;
20 }
23let greeter = new User("Patrick");

This example has a class function called "greet". This is the most common way to create function in TypeScript. It feels natural from people coming from C# because it's almost the same declaration. You start with the level of encapsulation you want, followed by the name of the function and the parameters and return type.

Under the hood, if you compile with EcmaScript 5 and lower, you will see:

1User.prototype.greet = function () {
2 return "Hello, " + this.name;

The default way to create function create a prototype function. A prototype JavaScript function is shared between all instance of your class, but the this point to the actual class. Hence, the value is not shared. Why TypeScript is using prototype by default is about performance. Prototype functions are faster and use less memory. So, now, you may have figure out that the next way to create function, the instance function will not use the prototype. And, that right. Instance function setup the method inside the instance. This means that if you have 1000 instances of your class that this method will be created 1000 times. Here is the output:

1this.assign = function (s, x) {
2 _this.name = s;
3 if (x === undefined) {
4 console.log("Undefined");
5 }

You can see that this has been changed to use _this which is defined by the closure of the instance of the class.

You can see the whole JavaScript generated by the TypeScript code above under this paragraph.

1var User = (function () {
2 function User(initialName) {
3 var_this = this;
4 this.assign = function (s, x) {
5 _this.name = s;
6 if (x === undefined) {
7 console.log("Undefined");
8 }
9 };
10 this.name = initialName;
11 }
12 User.prototype.greet = function () {
13 return "Hello, " + this.name;
14 };
15 User.assignStatic = function () {
16 User.count = User.count + 1;
17 };
18 return User;
20User.count = 0;
21var greeter = new User("Patrick");

The third way to create a function is by setting this one as static. A static function share the same information and function between every instance. The way it's translated is that it set the function at the root of the closure of the type. This is true for the function and for static variable.

When to use which one really depend of what you want to do. The easiest one to understand is static. You need static only if you want to share between instance data. While static may be a performance improvement in C# for method, it's still true in TypeScript BUT you already have that gain with the default class function that generate prototype function. So the main question is when to use an instance function. The main scenario is that this method will be used in a callback mechanism where the this pointer may get lost. While there is many pattern around like creating a wrapper function.

1class User {
2 private name: string;
3 constructor(initialName: string) {
4 this.name = initialName;
5 }
6 public greet(): void {
7 console.log("Hello, " + this.name);
8 }
9 public greet2 = (): void => {
10 console.log("Hello2, " + this.name);
11 };
12 public assign = (s: string, x?: string): void => {
13 this.name = s;
14 if (x === undefined) {
15 console.log("Undefined");
16 }
17 };
18 private static count = 0;
19 public static assignStatic() {
20 User.count = User.count + 1;
21 }
23let greeter = new User("Patrick");
24window.setTimeout(greeter.greet, 10); // this won't be the user this
25window.setTimeout(() => greeter.greet(), 1000); // this will be user this
26window.setTimeout(greeter.greet2, 2000); // this will be user this

Be aware that I changed a little bit the method and added a greet2 that is an instance function. You can see that I use inside these methods to log out the string. The output is as expected that the first one won't be able to output the name. The reason is that this is in the context of the window and not the user. Here is the output:

1Hello, Hello, Patrick Hello2, Patrick

A rule of thumb is to use class function as much as possible since they are optimized to not waste too much resource. About callback, using the arrow anonymous function is a great way if you do not need to release the pointer to this event. You may have to release memory manually and the easiest way in would be to use instance function. As you can see, it's not always black or white, but your line of though should be always to think about memory first.