Skip to main content
added 305 characters in body
Source Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k

You'd do it the same way is in ES5, with Object.defineProperty:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, // remove this line if not needed / wanted }); }); 

However, using a class just for static properties is an anitanti-pattern IMO. In that case you can as well use a plain object:

var AlphabetEnum = {}; 

You'd do it the same way is in ES5, with Object.defineProperty:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, }); }); 

However, using a class just for static properties is an anit-pattern IMO. In that case you can as well use a plain object:

var AlphabetEnum = {}; 

You'd do it the same way is in ES5, with Object.defineProperty:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, // remove this line if not needed / wanted }); }); 

However, using a class just for static properties is an anti-pattern IMO. In that case you can as well use a plain object:

var AlphabetEnum = {}; 
added 305 characters in body
Source Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k

You'd do it the same way is in ES5, with Object.defineProperty:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, }); }); 

However, using a class just for static properties is an anit-pattern IMO. In that case you can as well use a plain object:

var AlphabetEnum = {}; 

You'd do it the same way is in ES5:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, }); }); 

You'd do it the same way is in ES5, with Object.defineProperty:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, }); }); 

However, using a class just for static properties is an anit-pattern IMO. In that case you can as well use a plain object:

var AlphabetEnum = {}; 
Source Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k

You'd do it the same way is in ES5:

class AlphabetEnum {} ['a', 'b', 'c', ..., 'z'].forEach(letter => { Object.defineProperty(AlphabetEnum, letter.toUpperCase(), { get: () => letter, configurable: true, }); });