带有负函数的 Typescript 链常量
Typescript Chain Constants with a Negative function
我正在使用 Typescript 2.8.1 并希望将我的常量分组到一个公共文件中,类似于其他语言中的包含文件。虽然这是直截了当的,但我正在尝试链接一些方法来处理这些值,但似乎无法让它发挥作用。
编辑:为了简单起见,需要一个默认值 return 并纠正了我最初没有 return 对象的问题。
例如,我将数字 1 到 10 作为定义的常量:
export enum CONSTANTS {
ONE = 1, TWO, ... TEN
}
我希望能够在代码中使用这些,例如 CONSTANTS.FIVE
表示数字 5,但也可能做 CONSTANTS.NEGATIVE.FIVE
以获得 -5。
我正在尝试使用链式方法,但似乎我需要将原始枚举定义为 return 一个值的单独方法。
export class CONSTANTS {
private Value_:number;
public constructor() {
this.Value_ = 0;
}
public ONE() {
this.Value_ = 1;
return this;
}
public TWO() {
this.Value_ = 2;
return this;
}
public NEGATIVE() {
this.Value_ = this.Value_ * -1;
return this;
}
public GetValue() {
return this.Value_; // This is the function I want to default to at the end
}
value = new CONSTANTS().ONE().NEGATIVE(); // Trying for -1
离开 GetValue(); return对象。
作为替代方案,为了简化您的代码,您可以使用负一元运算符:
export enum CONSTANTS {
ONE = 1, TWO = 2, ... TEN
}
//**//
console.log(-CONSTANTS.TWO)
我正在使用 Typescript 2.8.1 并希望将我的常量分组到一个公共文件中,类似于其他语言中的包含文件。虽然这是直截了当的,但我正在尝试链接一些方法来处理这些值,但似乎无法让它发挥作用。
编辑:为了简单起见,需要一个默认值 return 并纠正了我最初没有 return 对象的问题。
例如,我将数字 1 到 10 作为定义的常量:
export enum CONSTANTS {
ONE = 1, TWO, ... TEN
}
我希望能够在代码中使用这些,例如 CONSTANTS.FIVE
表示数字 5,但也可能做 CONSTANTS.NEGATIVE.FIVE
以获得 -5。
我正在尝试使用链式方法,但似乎我需要将原始枚举定义为 return 一个值的单独方法。
export class CONSTANTS {
private Value_:number;
public constructor() {
this.Value_ = 0;
}
public ONE() {
this.Value_ = 1;
return this;
}
public TWO() {
this.Value_ = 2;
return this;
}
public NEGATIVE() {
this.Value_ = this.Value_ * -1;
return this;
}
public GetValue() {
return this.Value_; // This is the function I want to default to at the end
}
value = new CONSTANTS().ONE().NEGATIVE(); // Trying for -1
离开 GetValue(); return对象。
作为替代方案,为了简化您的代码,您可以使用负一元运算符:
export enum CONSTANTS {
ONE = 1, TWO = 2, ... TEN
}
//**//
console.log(-CONSTANTS.TWO)