TypeScript:将函数添加到枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28150739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript: Add functions to an Enum
提问by Mr. Baudin
Is it possible to add functions to an Enum type in TypeScript?
是否可以将函数添加到 TypeScript 中的 Enum 类型?
for example:
例如:
enum Mode {
landscape,
portrait,
// the dream...
toString() { console.log(this); }
}
Or:
或者:
class ModeExtension {
public toString = () => console.log(this);
}
enum Mode extends ModeExtension {
landscape,
portrait,
}
Of course the toString()
function would contain something like a switch
But a use-case would flow along the lines of:
当然,该toString()
函数将包含类似 aswitch
但用例将沿着以下路线流动:
class Device {
constructor(public mode:Mode) {
console.log(this.mode.toString());
}
}
I understand why extending an enum
might be a strange thing, just wondering if it is possible.
我明白为什么扩展一个enum
可能是一件奇怪的事情,只是想知道它是否可能。
回答by Fenton
You can either have a class that is separate to the Enum and use it to get things you want, or you can merge a namespace into the Enum and get it all in what looks like the same place.
您可以拥有一个与 Enum 分开的类并使用它来获取您想要的东西,或者您可以将一个命名空间合并到 Enum 中并将其全部放在看起来相同的地方。
Mode Utility Class
模式实用程序类
So this isn't exactly what you are after, but this allows you to encapsulate the "Mode to string" behaviour using a static method.
所以这并不是您所追求的,但这允许您使用静态方法封装“模式到字符串”行为。
class ModeUtil {
public static toString(mode: Mode) {
return Mode[mode];
}
}
You can use it like this:
你可以这样使用它:
const mode = Mode.portrait;
const x = ModeUtil.toString(mode);
console.log(x);
Mode Enum/Namespace Merge
模式枚举/命名空间合并
You can merge a namespace with the Enum in order to create what looks like an Enum with additional methods:
您可以将命名空间与 Enum 合并,以便使用其他方法创建看起来像 Enum 的内容:
enum Mode {
X,
Y
}
namespace Mode {
export function toString(mode: Mode): string {
return Mode[mode];
}
export function parse(mode: string): Mode {
return Mode[mode];
}
}
const mode = Mode.X;
const str = Mode.toString(mode);
alert(str);
const m = Mode.parse(str);
alert(m);
回答by Benjamin
You can get the string value of an non-const enum by using square brackets:
您可以使用方括号获取非常量枚举的字符串值:
class Device {
constructor(public mode:Mode) {
console.log(Mode[this.mode]);
}
}
You can also put some enum-specific util functions into the enum, but that's just like static class members:
您还可以将一些特定于枚举的 util 函数放入枚举中,但这就像静态类成员:
enum Mode {
landscape,
portrait
}
namespace Mode {
export function doSomething(mode:Mode) {
// your code here
}
}
回答by Dave Cousineau
Convert your enum
to the enum pattern. I find this is a better practice in general for many languages, since otherwise you restrict encapsulation options for your type. The tendency is to switch
on the enum value, when really any data or functionality that is dependent on the particular enum value should just go into each instance of the enum. I've added some more code to demonstrate.
将您转换enum
为枚举模式。我发现这对于许多语言来说通常是一种更好的做法,因为否则你会限制你的类型的封装选项。趋势是switch
在枚举值上,实际上任何依赖于特定枚举值的数据或功能都应该进入枚举的每个实例。我添加了更多代码来演示。
This might not work if you are particularly dependent on the underlying enum values. In that case you would need to add a member for the old values and convert places that need it to use the new property.
如果您特别依赖基础枚举值,这可能不起作用。在这种情况下,您需要为旧值添加一个成员并将需要它的位置转换为使用新属性。
class Mode {
public static landscape = new Mode(1920, 1080);
public static portrait = new Mode(1080, 1920);
public get Width(): number { return this.mWidth; }
public get Height(): number { return this.mHeight; }
// private constructor if possible in a future version of TS
constructor(
private mWidth: number,
private mHeight: number
) {
}
public GetAspectRatio() {
return this.mWidth / this.mHeight;
}
}
回答by david valentino
can make enum like by private constructor and static get return object
可以通过私有构造函数和静态获取返回对象进行枚举
export class HomeSlideEnum{
public static get friendList(): HomeSlideEnum {
return new HomeSlideEnum(0, "friendList");
}
public static getByOrdinal(ordinal){
switch(ordinal){
case 0:
return HomeSlideEnum.friendList;
}
}
public ordinal:number;
public key:string;
private constructor(ordinal, key){
this.ordinal = ordinal;
this.key = key;
}
public getTitle(){
switch(this.ordinal){
case 0:
return "Friend List"
default :
return "DChat"
}
}
}
then later can use like this
然后以后可以这样使用
HomeSlideEnum.friendList.getTitle();
回答by Stefanos Kargas
An addition to Fenton's solution. If you want to use this enumerator in another class, you need to export both the enum and the namespace. It would look like this:
Fenton 解决方案的补充。如果要在另一个类中使用此枚举器,则需要导出枚举和命名空间。它看起来像这样:
export enum Mode {
landscape,
portrait
}
export namespace Mode {
export function toString(mode: Mode): string {
return Mode[mode];
}
}
Then you just import the mode.enum.ts file in your class and use it.
然后你只需在你的类中导入 mode.enum.ts 文件并使用它。