Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: use private or public in constructor

I'm new to TypeScript world, and I've seen examples with this to deal with injected objects and set it to a property of the component (this.anything)

First with public and setting by hand to this.nav

export class XPTO {
   constructor(public nav: NavController) {
      this.nav = nav;
   }
}

and this, with private

export class XPTO {
   constructor(private nav: NavController) {
      //this.nav is nav?
   }
}

in both cases after construct the object this.nav is a NavController object. What are the differences of both implementations? Or this is the same when compiled to plain javascript?

like image 350
Christian Benseler Avatar asked Oct 03 '16 21:10

Christian Benseler


People also ask

Should constructor be public or private?

The constructor is not always declared as public, it can also be private, protected, or default. The private constructors prevent a class from fully and clearly expressed/represented by its callers. In that case private constructors are useful.

Can constructor be private in TypeScript?

I noticed that in Typescript you can define constructor to be with any access modifier (private, protected, public).

When would you use a private constructor?

Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.

What is protected constructor in TypeScript?

If you declare your constructor as protected, on the other hand, while your class still cannot be instantiated from external code, it can be extended. A protected constructor can be used by code within the class (like the private constructor) and also by code in a class that extends it.


2 Answers

Actually in your first example the explicit assignment is not needed at all:

export class XPTO {
   constructor(public nav: NavController) {
       // This line is not required.
       // this.nav = nav;
       this.someFunction();
   }
   someFunction(){
       console.log(this.nav); // Prints out the NavController.
   }
}

Whenever you specify public or private on a constructor parameter a corresponding public/private variable is created on the class and filled with the value of the parameter.

So really, the only difference of the two code samples is that one is private and the other one is public.

The resulting JavaScript will be the same. However, the compiler will throw an error, if you are trying to access private variables in your code.

like image 126
Pape Avatar answered Sep 22 '22 23:09

Pape


public and private, as a lot of Typescript features, are only TypeScript modifiers. I'm not sure the compiler names these variables exactly the same, but from a JavaScript point of view, the code will be essentially the same.

The interest of Typescript is to give you features like type checking, it doesn't necessarily always modifies the outputted code.

like image 31
Kewin Dousse Avatar answered Sep 24 '22 23:09

Kewin Dousse