By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

What do you use instead of enum?

P: n/a
I wanted to do a state machine with symbolic states, but I see enum is
not part of the standard. I only have seven, do I just make seven vars
and hand enumerate?

kt
Oct 14 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Oct 14, 5:15*am, Kenny <kentil...@gmail.comwrote:
I wanted to do a state machine with symbolic states, but I see enum is
not part of the standard. I only have seven, do I just make seven vars
and hand enumerate?

kt
http://dean.edwards.name/weblog/2006/07/enum/
Oct 14 '08 #2

P: n/a
On 2008-10-14 16:17, Laser Lips wrote:
On Oct 14, 5:15 am, Kenny <kentil...@gmail.comwrote:
>I wanted to do a state machine with symbolic states, but I see enum is
not part of the standard. I only have seven, do I just make seven vars
and hand enumerate?

http://dean.edwards.name/weblog/2006/07/enum/
I don't think that's what he was talking about; it sounds more like C's
enum declarations:

enum MYSTATES {
state_1,
state_2,
..
} state;

Since there are no user-definable constants in ECMAScript (yet), enums
don't make much sense. Just use variables and assign the numbers
yourself, or roll your own function that does enumeration. Mozilla has
had non-standard "const" declarations for some time, and Opera has
implemented them too. They're obviously not portable, but YMMV.
- Conrad
Oct 14 '08 #3

P: n/a
Kenny wrote:
Laser Lips wrote:
>On Oct 14, 5:15 am, Kenny <kentil...@gmail.comwrote:

I went with (locally to my state machine):

var left=0,top=1,right=2,bottom=3;
As for style, I might rather use constant case.

var LEFT = 0,
TOP = 1,
RIGHT = 2,
BOTTOM = 3;

Garrett
>
kt

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
Oct 14 '08 #4

P: n/a
dhtml wrote:
Kenny wrote:
>Laser Lips wrote:
>>On Oct 14, 5:15 am, Kenny <kentil...@gmail.comwrote:


>I went with (locally to my state machine):

var left=0,top=1,right=2,bottom=3;

As for style, I might rather use constant case.

var LEFT = 0,
TOP = 1,
RIGHT = 2,
BOTTOM = 3;
good point. I did that in my C days as well.

I see JS itself supports const, but not ECMAScript. I gather folks stick
with the latter standard to avoid browser-battling?

Food for flamewar: how much Web developer energy is wasted on browsers
not being compatible? Frightening thought, adding up all the
person-hours. For my money the Web is about the coolest thing
civilization has ever done, and the browsers (esp. IE, I hear) are a
huge sea-anchor on the whole deal.

kt
Oct 14 '08 #5

P: n/a
In comp.lang.javascript message <48**********************@cv.net>, Tue,
14 Oct 2008 11:26:30, Kenny <ke*******@gmail.composted:
>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
Oct 14 '08 #6

P: n/a
On 2008-10-14 23:26, Kenny wrote:
I see JS itself supports const, but not ECMAScript. I gather folks stick
with the latter standard to avoid browser-battling?
ECMAScript is the underlying standard for all of the implementations; it
has reserved the "const" keyword for possible future use.
Netscape/Mozilla's JavaScript is one of many implementations of
ECMAScript, and they've decided to implement "const". Personally, I
welcome that, but as long as it's not portable, it's not much use
outside of closed environments like intranets.
Food for flamewar: how much Web developer energy is wasted on browsers
not being compatible? Frightening thought, adding up all the
person-hours.
Not much food for a flame war, I think everybody is well aware of the
cost, except maybe Microsoft. Here's a breakdown from a related field
(web design), that made me chuckle and cry at the same time:

http://www.flickr.com/photos/mringlein/316476217/
For my money the Web is about the coolest thing
civilization has ever done, and the browsers (esp. IE, I hear) are a
huge sea-anchor on the whole deal.
I agree 100%.
- Conrad
Oct 14 '08 #7

P: n/a
Conrad Lender wrote:
On 2008-10-14 23:26, Kenny wrote:
>>I see JS itself supports const, but not ECMAScript. I gather folks stick
with the latter standard to avoid browser-battling?


ECMAScript is the underlying standard for all of the implementations; it
has reserved the "const" keyword for possible future use.
Netscape/Mozilla's JavaScript is one of many implementations of
ECMAScript, and they've decided to implement "const". Personally, I
welcome that, but as long as it's not portable, it's not much use
outside of closed environments like intranets.

>>Food for flamewar: how much Web developer energy is wasted on browsers
not being compatible? Frightening thought, adding up all the
person-hours.


Not much food for a flame war, I think everybody is well aware of the
cost, except maybe Microsoft.
Here's a breakdown from a related field
(web design), that made me chuckle and cry at the same time:

http://www.flickr.com/photos/mringlein/316476217/
Nice/ I had a feeling I was about the last to discover this. :) I am
pleased that I quickly figured out the bit about Just Use a Table
instead of wrestling with CSS. (Tho the tables are so ghastly I can tell
I am not done yet on this issue.)

It is a shame about the slices for Microsoft and Bill. What was the
point for Bill? Now he is frustrated trying to give it away effectively.
With Sir Thomas More's rebuke in A Man For All Seasons: "It profits a
man nothing if he gains the world and loses his soul, Bill, but for a
monopoly on desktop computing?"
>
>>For my money the Web is about the coolest thing
civilization has ever done, and the browsers (esp. IE, I hear) are a
huge sea-anchor on the whole deal.


I agree 100%.
OK, let's have Al Gore finish creating the Internet after he finishes
with global warming.

:)

kt
Oct 15 '08 #8

P: n/a
Kenny wrote:
I wanted to do a state machine with symbolic states, but I see enum is
not part of the standard. I only have seven, do I just make seven vars
and hand enumerate?
You can use strings. The strings are used as keys to select actions and
transitions from an object that can be built with an object literal.

{
go: function () {
state = 'ok';
},
ovalue: function () {
state = 'ocomma';
},
firstavalue: function () {
state = 'acomma';
},
avalue: function () {
state = 'acomma';
}
}

See http://www.json.org/json_parser_state.js for an example.
Oct 15 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.