枚举类型的方法

时间:2020-05-27 11:26:40

标签: javascript

假设我有一个这样的枚举:

const Days = {
  MONDAY: `MONDAY`,
  TUESDAY: `TUESDAY`,
  WEDNESDAY: `WEDNESDAY`
  //, etc.
};

var aDay = Days.MONDAY;

我该如何实现像“ aDay.next();”这样的“ next()”函数?

编辑:这是我的操作,遵循我在评论中得到的一些建议。我想创建一个父类Enum,以便可以以一种优雅的方式定义所有不同的枚举,并且在我的代码中没有冗余。 Here is the JSFiddle。请让我知道您对此有何看法以及我应该修改的内容:

class Enum {
  constructor(value) {
    this.value = value;
  }

  value() {
    return this.value;
  }

    get index() {
    // this.constructor returns the class DayOfWeek to call the static variable/function values
    return this.constructor.values.indexOf(this);
  }

  get next() {
    // this.constructor returns the class DayOfWeek to call the static variable/function values
    return this.constructor.values[(this.index + 1) % this.constructor.values.length];
  }

  get prev() {
    // this.constructor returns the class DayOfWeek to call the static variable/function values
    let unchecked = (this.index - 1) % this.constructor.values.length;
    return this.constructor.values[unchecked < 0 ? unchecked + this.constructor.values.length : unchecked];
  }
}

class DayOfWeek extends Enum {}

DayOfWeek.MONDAY =    new DayOfWeek("Monday");
DayOfWeek.TUESDAY =   new DayOfWeek("Tuesday");
DayOfWeek.WEDNESDAY = new DayOfWeek("Wednesday");
DayOfWeek.THURSDAY =  new DayOfWeek("Thursday");
DayOfWeek.FRIDAY =    new DayOfWeek("Friday");
DayOfWeek.SATURDAY =  new DayOfWeek("Saturday");
DayOfWeek.SUNDAY =    new DayOfWeek("Sunday");
DayOfWeek.values = [DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY];

1 个答案:

答案 0 :(得分:0)

假设您的目标是ES2015或更高版本,则可以使用

class DayOfWeek {
    constructor(index) {
        this.index = index;
        this.names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
        // TODO: Add validation that number is between 1 and 7
    }
    get name() {
        return this.names[this.index];
    }
    next() {
        let next = this.index + 1;
        if (next === 7) {
            next = 1;
        }
        return Days[this.names[next]];
    }
}
export class Days {
}
Days.Monday = new DayOfWeek(0);
Days.Tuesday = new DayOfWeek(1);
Days.Wednesday = new DayOfWeek(2);
Days.Thursday = new DayOfWeek(3);
Days.Friday = new DayOfWeek(4);
Days.Saturday = new DayOfWeek(5);
Days.Sunday = new DayOfWeek(6);

或者,如果您使用的是TypeScript,则可以这样编写,然后编译为更高级的JS

class DayOfWeek {
  private names: string[] = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
  constructor(private index: number) {
    // TODO: Add validation that number is between 1 and 7
  }

  public get name(): string {
    return this.names[this.index];
  }

  public next(): DayOfWeek {
    let next = this.index + 1;
    if(next === 7) {
      next = 1;
    }

    return Days[this.names[next]];
  }
}

export class Days {
  public static Monday: DayOfWeek = new DayOfWeek(0);
  public static Tuesday: DayOfWeek = new DayOfWeek(1);
  public static Wednesday: DayOfWeek = new DayOfWeek(2);
  public static Thursday: DayOfWeek = new DayOfWeek(3);
  public static Friday: DayOfWeek = new DayOfWeek(4);
  public static Saturday: DayOfWeek = new DayOfWeek(5); 
  public static Sunday: DayOfWeek = new DayOfWeek(6);  
}