The Angular 2 guide has this example with a constructor in a
component://Typescript
class DisplayComponent {
myName: string;
names: Array<string>;
constructor() {
this.myName = "Alice";
this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];
}
}
From https://angular.io/docs/ts/latest/guide/displaying-data.html#Create-an-array (retrieved 2015-10-30)
Consider this is a simple example, but often in real life, my constructors grow bigger and bigger. Also I have sometimes the need to use the same logic several places, and not only on construction time.
My first reaction was to create a separate function and call that on the instance. But Typescript does not allow this, since the instance is not created when the constructor runs.
My question is: How do I extract lines of code from the constructor and pull it out into a separate function?