Delonous
Delonous

Reputation: 201

Exporting classes with a constructor vs. no constructor

Using Angular2 with typescript. Ran into this question about constructors and classes I couldn't find an answer to.

So I was trying to figure out the difference between these two pieces of code. Not sure if one is better to use in practice. Thanks

export class ONE {
     id: number;
     name: string;
}

and

export class TWO {

  constructor(
    public id: number,
    public name: string,
    ) {  }
}

Upvotes: 1

Views: 57

Answers (1)

toskv
toskv

Reputation: 31612

None. Well apart from the parameter initialization of course.

You can easily check that by looking at the generated code.

var ONE = (function () {
    function ONE() {
    }
    return ONE;
}());
var TWO = (function () {
    function TWO(id, name) {
        this.id = id;
        this.name = name;
    }
    return TWO;
}());

You can easily check that in the TypeScript playground.

Upvotes: 2

Related Questions