有没有一种方法可以最小化Javascript中的类定义



我想在节点应用程序中使用类,但我想知道是否有一种方法(如typescript(可以最小化类定义,例如:

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

类似

class Rectangle(
constructor(height, width){}
)

所以我不需要将构造函数中的值分配给具有完全相同名称的变量。

否,参数属性只是一个typescript特性。正如你在ts操场上的这个例子中所看到的,像这样的类

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

被传送到:

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

然而,你可以在你的NodeJS应用程序中使用TypeScript——假设你设置了一个构建步骤,将你的TypeScript代码转换/编译成javascript。或者,您可以使用类似ts节点的库来运行代码。

最新更新