What do you use instead of enum?

K

Kenny

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
 
K

Kenny

Laser said:

Nice, but that is not the enum I had in mind.

C supports, eg:

enum {left, top, right, bottom};

after which, eg, right==2.

Apparently ecmascript /reserved/ enum in case they got around to
implementing it, but have not yet. I was wondering if there was some
clever hack that hardcore JS types use as a subsitute.

I went with (locally to my state machine):

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

Not the end of the world.

kt
 
D

dhtml

Kenny said:
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
 
K

Kenny

dhtml said:
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
 
D

Dr J R Stockton

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.
 
K

Kenny

Conrad said:
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.




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?"
I agree 100%.

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

:)

kt
 
D

Douglas Crockford

Kenny said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top