Tue said:
C supports, eg:
enum {left, top, right, bottom};
after which, eg, right==2.
I was wondering whether it might be done in JavaScript by writing that as
enumm ("left, top, right, bottom");
and having a function enumm which edited that string into a string or
strings such as "left=0", "top=1", "right=2; bottom=3;" and then using
eval. That could be a new use for the "eval" section of the FAQ.
This one-liner function works in my Firefox 2.0.0.17, and Opera 9.27, and
Safari 3.1.2, and in Chrome and IE7 if top is mis-spelt topp .
function enumm(S) { for (var J=0, A=S.split(/\W+/), L=A.length ; J<L ; J++) eval( A[J] + "=" + J ) }
enumm ("left, top, right, bottom")
alert(bottom + " # " + top)
In case it matters : that's testing in my js-quick.htm, which executes
it by calling a function which calls eval.
By adding code to enumm, non-contiguous enumeration could be supported :
enumm ("left, top, right=77, bottom"); // bottom==78
and the string could include variable references ... .
Your move.