You can use destructuring assignment to define enumerations in ES6 as follows:
var [red, green, blue] = [0, 1, 2];
Instead, I'd like the right hand side of the destructuring assignment to be dynamic. For example:
var MAX_ENUM_SIZE = 32;
var ENUM = new Array(MAX_ENUM_SIZE);
for (var i = 0; i < MAX_ENUM_SIZE; i++) ENUM[i] = i;
var [red, green, blue] = ENUM;
Unfortunately, this seems like a hack. What if I want a bigger enumeration in the future? Hence, I was thinking of using destructuring assignment with an iterator as follows:
var [red, green, blue] = enumeration(/* I don't want to specify size */);
However, I don't think it's possible to use destructuring assignment with iterators[citation needed]. Is there any way to acplish this goal?
You can use destructuring assignment to define enumerations in ES6 as follows:
var [red, green, blue] = [0, 1, 2];
Instead, I'd like the right hand side of the destructuring assignment to be dynamic. For example:
var MAX_ENUM_SIZE = 32;
var ENUM = new Array(MAX_ENUM_SIZE);
for (var i = 0; i < MAX_ENUM_SIZE; i++) ENUM[i] = i;
var [red, green, blue] = ENUM;
Unfortunately, this seems like a hack. What if I want a bigger enumeration in the future? Hence, I was thinking of using destructuring assignment with an iterator as follows:
var [red, green, blue] = enumeration(/* I don't want to specify size */);
However, I don't think it's possible to use destructuring assignment with iterators[citation needed]. Is there any way to acplish this goal?
Share Improve this question asked Sep 24, 2016 at 7:12 Aadit M ShahAadit M Shah 74.3k31 gold badges175 silver badges307 bronze badges 7- Why is this a good idea? – samsonthehero Commented Sep 24, 2016 at 7:18
- you need only the first three values with only three target variables, – Nina Scholz Commented Sep 24, 2016 at 7:22
- Less brittle code? If somebody adds a new alternative to the enumeration and forgets to update the right hand side then you might get an error. However, if the right hand side is dynamic then you don't have to worry about that. – Aadit M Shah Commented Sep 24, 2016 at 7:22
-
Are you trying to dynamically create variable names to correspond to an unknown number of elements returned by
enumeration
? – guest271314 Commented Sep 24, 2016 at 7:23 - @AaditMShah you want the enumerations to match the number of var names specified on the LHS ? – Mulan Commented Sep 24, 2016 at 7:23
2 Answers
Reset to default 13Use a generator
function* enumerator() {
let i = 0;
while (true) yield i++;
};
let [red,green,blue] = enumerator();
console.log(red, green, blue); // 0 1 2
let [a,b,c,d,e] = enumerator();
console.log(a,b,c,d,e); // 0 1 2 3 4
The generator is flexible making this pretty neat for implementing different types of enums – for example, these cute bitmask enums
function* bitmask() {
let i = 0;
while (i < 32) yield 1 << i++;
throw Error("bitmask enumerator exceeds 32 bits");
}
let [R,W,X] = bitmask();
const read = p => (p & R) !== 0;
const write = p => (p & W) !== 0;
const exec = p => (p & X) !== 0;
{
let p = R | W; // read and write only
console.log("can read?", read(p)); // true
console.log("can write?", write(p)); // true
console.log("can exec?", exec(p)); // false
}
{
let p = R | X; // read and execute only
console.log("can read?", read(p)); // true
console.log("can write?", write(p)); // false
console.log("can exec?", exec(p)); // true
}
Note, each of the below prospective approaches could probably be improved.
The variables appear to be global at Question. You can create an array of strings referencing the variable which should be created, define the variable from the element of the array
// not technically destructuring, though achieves same result;
// that is, setting variables globally
for (var prop of (props = ["a", "b", "c", "d"])) {
// set identifier globally
self[prop] = props.indexOf(prop); // set a value for each `prop`
}
// delete `prop`, `props`
prop = props = void 0;
console.log(a, b, c, d);
Other approaches
using object destructuring
var {
red, blue, green
} = (function(data, props) {
for (var prop of Object.keys(props)) {
data[props[prop]] = props.indexOf(props[prop]); // or some other value
};
return data
}({}, ["red", "blue", "green"]));
console.log(red, blue, green);
using a list of variables to be
var props = ["red", "blue", "green"]; // list of variables to be
var [red, blue, green] = props.map((v, k) => k);
console.log(red, blue, green, "window[\"red\"]:", window[props[0]]);