In comp.lang.javascript message <48f4ba43$0$4919$607ed4bc@cv.net>, Tue,
14 Oct 2008 11:26:30, Kenny <kentilton@gmail.composted:
Quote:
>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.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk BP7, Delphi 3 & 2006.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htmclpdmFAQ;
NOT <URL:http://support.codegear.com/newsgroups/>: news:borland.* Guidelines