M
M
Maks Burkov2017-05-21 15:57:26
JavaScript
Maks Burkov, 2017-05-21 15:57:26

Does this code from Type Script compile correctly?

Due to the inability to understand the object orientation of java script, I decided to switch to type script and in parallel to look at what is happening with java script in this way, since I came from java, then type script felt like home for me.
1. Does this code compile correctly to Java Script?
(Question to Java Script professionals) ..
2. Is it possible to write this code compiled into Java Script in a simpler way?
3. How does the use of Type Script affect the speed compared to the code written in the original Js?
I use Webstorm.
Ts:

class Engine {
    constructor(public horsePower:number,public engineType:string){}
}

class Car {
    private _engine:Engine;
    constructor(engine:Engine){
        this._engine = engine;
    }
    get getEngine():Engine{
        return this._engine;
    }
}

window.onload = function () {
    document.write("Document was loaded!");
    var engine = new Engine(300,'v8');
    var car = new Car(engine);
    document.write("Car Engine type: "+car.getEngine.engineType);
    document.write("Car horse power: "+car.getEngine.horsePower.toString());
}

js:
var Engine = (function () {
    function Engine(horsePower, engineType) {
        this.horsePower = horsePower;
        this.engineType = engineType;
    }
    return Engine;
}());
var Car = (function () {
    function Car(engine) {
        this._engine = engine;
    }
    Object.defineProperty(Car.prototype, "getEngine", {
        get: function () {
            return this._engine;
        },
        enumerable: true,
        configurable: true
    });
    return Car;
}());
window.onload = function () {
    document.write("Document was loaded!");
    var engine = new Engine(300, 'v8');
    var car = new Car(engine);
    document.write("Car Engine type: " + car.getEngine.engineType);
    document.write("Car horse power: " + car.getEngine.horsePower.toString());
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question