1

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?

5
  • 2
    The JavaScript effectively is exactly what you suggest. Note that the value of Car will be the return value from that anonymous function, and that anonymous function is executed as part of the var initialization. Thus Car ends up being exactly that small function you think it should be. Commented Dec 6, 2016 at 14:40
  • 1
    Are you asking why it's being wrapped in a function execution and assigned to a variable instead of just having the function? Commented Dec 6, 2016 at 14:41
  • 1
    Now, why is it that way? It's code produced by a transpiler. In the general case of translating TypeScript class declarations, that structure makes sense. Commented Dec 6, 2016 at 14:41
  • Thank you guys for clarification. I guess I looked into the equivalent code and did not even think for a moment that it might be just from the transpiler. Commented Dec 6, 2016 at 14:44
  • Also, when targeting es6 the compiler will keep the class syntax as it is supported in es6 javascript Commented Dec 6, 2016 at 14:50

1 Answer 1

2

You are right but there is a reason for the TypeScript code to look like that...

It becomes more obvious once you add some methods to your class:

class Car {  
    engine: Engine;
    constructor(engine: Engine) {
        this.engine = engine;
    }
    drive() { 
        this.engine.start();
    }
}

The output JS look as follows:

var Car = (function () {
    function Car(engine) {
        this.engine = engine;
    }
    Car.prototype.drive = function () {
        this.engine.start();
    };
    return Car;
}());

As you can see TS uses a IIFE to wrap the entire class declaration.

It is a nice way to keep everything together.

Another example using decorators:

@decorate
class Car {  
    engine: Engine;
    constructor(engine: Engine) {
        this.engine = engine;
    }
    drive() { 
        this.engine.start();
    }
}

And the output JS:

var Car = (function () {
    function Car(engine) {
        this.engine = engine;
    }
    Car.prototype.drive = function () {
        this.engine.start();
    };
    Car = __decorate([
        decorate
    ], Car);
    return Car;
}());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.