I am starting with typescript and I came across John Papa's tutorial. He had the following:
// TypeScript
class Car {
// Property (public by default)
engine: string;
// Constructor
// (accepts a value so you can initialize engine)
constructor(engine: string) {
this.engine = engine;
}
}
the equivalent js code:
// JavaScript
var Car = (function () {
function Car(engine) {
this.engine = engine;
}
return Car;
})();
It got me confused. Shouldn't it be:
function Car(engine) {
this.engine = engine;
}
Am I missing something here?
Carwill be the return value from that anonymous function, and that anonymous function is executed as part of thevarinitialization. ThusCarends up being exactly that small function you think it should be.classdeclarations, that structure makes sense.es6the compiler will keep the class syntax as it is supported in es6 javascript