3

Does somebody know how do I write this Javascript code into Typescript? Especially the prototype inside of the class causes me problems...

var Module = (function () {
    function Module(name) {
       this.name = name;
    }
    Module.prototype.toString = function () {
       return this.name;
    };
    return Module;
})();

var Student = (function () {
    function Student(name, studentNumber) {
         this.bookedModules = [];
         this.name = name;
         this.studentNumber = studentNumber;
}
Student.prototype.bookModule = function (bookedModule) {
      this.bookedModules.push(bookedModule);
};
Student.prototype.bookedModuleNames = function () {
      return this.bookedModules.map(function (module) {
            return module.toString();
      });
    };
    return Student;
})();
0

2 Answers 2

2

In typescript you use classes, the compiler will do the prototype work for you.
You code is equivalent to:

class Module {
    public name: string;

    constructor(name: string) {
        this.name = name;
    }

    toString(): string {
        return this.name;
    }
}

class Student {
    public name: string;
    public studentNumber: number;
    public bookedModules: Module[];

    constructor(name: string, studentNumber: number) {
        this.name = name;
        this.bookedModules = [];
        this.studentNumber = studentNumber;
    }

    bookModule(book: Module): void {
        this.bookedModules.push(book);
    }

    bookedModuleNames(): string[] {
        return this.bookedModules.map(book => book.name);
    }
}

(code in playground)

Which compiles into:

var Module = (function () {
    function Module(name) {
        this.name = name;
    }
    Module.prototype.toString = function () {
        return this.name;
    };
    return Module;
}());
var Student = (function () {
    function Student(name, studentNumber) {
        this.name = name;
        this.bookedModules = [];
        this.studentNumber = studentNumber;
    }
    Student.prototype.bookModule = function (book) {
        this.bookedModules.push(book);
    };
    Student.prototype.bookedModuleNames = function () {
        return this.bookedModules.map(function (book) { return book.name; });
    };
    return Student;
}());
Sign up to request clarification or add additional context in comments.

Comments

0

Use classes - typescript will generate this code for you:

class Module {
    constructor(public name) {
    }

    toString() {
        return this.name;
    }
}

class Student {
    bookedModules: Module[];   

    constructor(public name, public studentNumber) {
        this.bookedModules = [];
    }

    bookModule(bookedModule: Module) {
        this.bookedModules.push(bookedModule);
    } 

    //...
}

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.