What do you use instead of enum?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kenny

    What do you use instead of enum?

    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
  • Laser Lips

    #2
    Re: What do you use instead of enum?

    On Oct 14, 5:15 am, Kenny <kentil...@gmai l.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

    Comment

    • Conrad Lender

      #3
      Re: What do you use instead of enum?

      On 2008-10-14 16:17, Laser Lips wrote:
      On Oct 14, 5:15 am, Kenny <kentil...@gmai l.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

      Comment

      • dhtml

        #4
        Re: What do you use instead of enum?

        Kenny wrote:
        Laser Lips wrote:
        >On Oct 14, 5:15 am, Kenny <kentil...@gmai l.comwrote:
        >>

        I went with (locally to my state machine):
        >
        var left=0,top=1,ri ght=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.javas cript FAQ <URL: http://jibbering.com/faq/ >

        Comment

        • Kenny

          #5
          Re: What do you use instead of enum?

          dhtml wrote:
          Kenny wrote:
          >
          >Laser Lips wrote:
          >>
          >>On Oct 14, 5:15 am, Kenny <kentil...@gmai l.comwrote:
          >>>
          >
          >
          >
          >I went with (locally to my state machine):
          >>
          > var left=0,top=1,ri ght=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

          Comment

          • Dr J R Stockton

            #6
            Re: What do you use instead of enum?

            In comp.lang.javas cript message <48f4ba43$0$491 9$607ed4bc@cv.n et>, Tue,
            14 Oct 2008 11:26:30, Kenny <kentilton@gmai l.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.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
            <URL:http://www.bancoems.co m/CompLangPascalD elphiMisc-MiniFAQ.htmclpd mFAQ;
            NOT <URL:http://support.codegea r.com/newsgroups/>: news:borland.* Guidelines

            Comment

            • Conrad Lender

              #7
              Re: What do you use instead of enum?

              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:


              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

              Comment

              • Kenny

                #8
                Re: What do you use instead of enum?

                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:
                >

                >
                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
                >>civilizatio n 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

                Comment

                • Douglas Crockford

                  #9
                  Re: What do you use instead of enum?

                  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.

                  Comment

                  Working...