BeingSuman
BeingSuman

Reputation: 3323

What is difference between instantiating variable inside constructor and directly in class + Typescript / Angular

export class ReactiveFormOne {
  studentA: Student = new Student();

  studentB: Student;

  constructor (){
    this.studentB = new Student();
  }
}

What is the fundamental difference between studentA and studentB?

Upvotes: 0

Views: 91

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220994

These are effectively identical. A class property initializer is transpiled into an equivalent assignment following the first super call, or the first statement(s) of the constructor if there is no super call.

I would recommend putting order-dependent initializations in the constructor body since future maintainers will generally be less eager to re-order statements in blocks, but may e.g. re-order initialized properties declared in the class body to conform to style guides.

Upvotes: 2

Related Questions