Is there a way to minimize class definitions in Javascript?

Viewed 122

I want to use classes in my Node Application but I wonder whether there is a way (like in typescript) to minimize class definitions, for example:

class Rectangle(

    constructor(height, width){
        this.height = height;
        this.width = width;
    }
)

to something like

class Rectangle(

    constructor(height, width){}
)

so I do not need to assign the values from the constructor to variables with exactly the same name.

1 Answers

No, parameter properties are a typescript only feature. As you can see in this example on ts-playground, a class like

class Rectangle{
   constructor(private height:number, public width:number){ }
}

gets transpiled into:

"use strict";
class Rectangle {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

You can, however, just use TypeScript in your NodeJS app - given that you set up a build-step where you transpile/compile your typescript code into javascript. Alternatively you can use a library like ts-node to run your code.

Related