LoginSignup
14
14

More than 5 years have passed since last update.

ES2015 で Enum っぽいもの

Posted at

Symbols を利用します。例えば「曜日」を表現する場合、

week.es6
const week = {
  SUM: Symbol(),
  MON: Symbol(),
  TUE: Symbol(),
  WED: Symbol(),
  THU: Symbol(),
  FRI: Symbol(),
  SAT: Symbol()
};

export default week;

この week.◯◯◯ は一意であり、他の方法で同値のものを生成できません。

利用したい場所で week.es6import して利用します。

some.es6
import week from './week.es6';

const myWeek = week.FRI;

if (myWeek === week.FRI) {
  console.log('金曜日です');
}
14
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14