About constructor parameters

hello everyone.

I have a question about constructor. and I can’t understand it’s fundamentals. I just using it because it works.

when i make a ts code, i use like

constructor(public navCtrl: NavController, public http: Http)

and I can use navCtrl & http.
yes. I import these module before, but how that can makes?

before I am a C, JAVA programmer so, I can’t understand these parameters.
when i use java, i make these code inside constructor or get from other class, but in typescript I can use just like that

how these work?? thanks

Any time you add an access qualifier (private or public) to a constructor parameter, TypeScript behind the scenes declares an object property of that name.

1 Like

@rapropos
hi rapropos. thanks for your reply. and, you means that I can use declare Http http instead of constructor parameter?

Equivalent:

class Foo {
  http: Http;

  constructor(http: Http) {
    this.http = http;
  }
}
class Foo {
  constructor(public http: Http) {
  }
}

Behind the scenes, tsc converts the second code into the equivalent of the first one. I find the second more readable.

oh thanks @rapropos. I understand all.