JavaScript: Trouble with switch Syntax

Discussion in 'Javascript' started by Gene Wirchenko, Dec 14, 2011.

  1. Dear JavaScripters:

    I have hanged myself yet again with syntax.

    Did you know that -- in IE9, at least -- you can use
    otherwise
    instead of
    default:
    in a switch statement?

    Yes, you can. It does not throw an error, but it also does not
    do anything. Unless you count time wasted looking for a bug.

    I may as well ask if anyone else has committed such bogosities.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Dec 14, 2011
    #1
    1. Advertising

  2. Gene Wirchenko

    Tim Streater Guest

    In article <-berlin.de>,
    Jake Jarvis <> wrote:

    > On 14.12.2011 22:23, Gene Wirchenko wrote:
    > > Dear JavaScripters:
    > >
    > > I have hanged myself yet again with syntax.
    > >
    > > Did you know that -- in IE9, at least -- you can use
    > > otherwise
    > > instead of
    > > default:
    > > in a switch statement?
    > >
    > > Yes, you can. It does not throw an error, but it also does not
    > > do anything. Unless you count time wasted looking for a bug.
    > >
    > > I may as well ask if anyone else has committed such bogosities.
    > >

    >
    > Do you mean something like the following?
    >
    > switch (foo) {
    > case bar:
    > yip;
    > break;
    > otherwise
    > yap;
    > break;
    > }
    >
    > 'otherwise' is a statement that'd belong to the list of statements
    > associated with 'case bar:' and be unreachable code because placed after
    > a break.
    >
    > If you meant 'otherwise:', it'd be a label for the 'yap;' statement and
    > thus belong to the list of statements associated with the 'case bar:',
    > and be unreachable code.
    >
    >
    >
    > Someone evaluating a switch statement must act on parts that go from
    > each 'case expr:' or 'default:' to right before the next one, what
    > follows after the ':' will be lumped into a list of statements to be
    > evaluated for a given case
    >
    > switch (foo) {
    >
    > case bar: // { bar's list
    > yip;
    > break;
    > otherwise
    > yap;
    > break;
    > // }
    >
    > case baz: // { baz's list
    > yop;
    > break;
    > // }
    >
    > default: // { default's list
    > yup;
    > break;
    > // }
    >
    > }


    IOW, replace 'otherwise' by 'ahScrewThis' or whatever takes your fancy,
    for similar effect.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 14, 2011
    #2
    1. Advertising

  3. Gene Wirchenko

    Aaron Sawyer Guest

    "Gene Wirchenko" <> wrote in message
    news:...
    > Dear JavaScripters:
    >
    > I have hanged myself yet again with syntax.
    >
    > Did you know that -- in IE9, at least -- you can use
    > otherwise
    > instead of
    > default:
    > in a switch statement?
    >
    > Yes, you can. It does not throw an error, but it also does not
    > do anything. Unless you count time wasted looking for a bug.
    >
    > I may as well ask if anyone else has committed such bogosities.
    >

    Not that particular one, but plenty of others.

    'otherwise' (without the quotes) on a line by itself, presumably following a
    'break;'will trigger Javascript/JScript/ECMAscript's default behaviors:
    (1) 'otherwise' is not a reserved word, therefore it is an identifier;
    (2) the identifier 'otherwise' is not declared locally (in a 'var'
    statement), therefore it must be a property attached to the global object
    (and created there if not found);
    (3) end of line has been encountered, therefore a semicolon must have been
    intended (!) and will be supplied by the language processor.

    That 'otherwise' has successfully instantiated a property on the global
    object with initial value 'undefined', which is consumed in place by the
    semicolon (basically, it is an expression whose value is ignored). Not a
    problem!

    If it had been the case that 'otherwise' had a colon following it
    (otherwise:), then that would have been a statement label; again, no problem
    at all!

    Such a nice language processor, so eager to please!

    The syntax of Javascript draws heavily from the C language (not Pascal). I
    use Firefox a lot, and I have found the Mozilla Developer Network website
    particularly helpful:

    https://developer.mozilla.org/en/JavaScript/Reference

    For JScript, the Microsoft Developer Network is a good start point:

    http://msdn.microsoft.com/en-us/library/yek4tbz0(v=VS.85).aspx

    and W3C gets me pointed at DOM/XML/XSL :

    http://www.w3.org/standards/

    I also spent time looking at
    http://www.jslint.com/ <<--!! very useful !!
    http://javascript.crockford.com/
    http://helephant.com/2008/08/17/how-javascript-objects-work/
    http://jibbering.com/faq/notes/closures/
    http://bonsaiden.github.com/JavaScript-Garden/

    c.l.j contributors John Harris and John Stockton also have online resources
    available.
    There are doubtless others as well, and I intend no slight by my lack of
    knowledge.

    Learn and grow, share and enjoy!
     
    Aaron Sawyer, Dec 14, 2011
    #3
  4. On Wed, 14 Dec 2011 18:58:55 -0400, "Aaron Sawyer"
    <> wrote:

    [snip]

    >Such a nice language processor, so eager to please!


    An error message letting me know that I had botched would have
    pleased me.

    [snip]

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Dec 15, 2011
    #4
  5. On Wed, 14 Dec 2011 23:32:47 +0100, Jake Jarvis
    <> wrote:

    [snip]

    >Do you mean something like the following?
    >
    >switch (foo) {
    > case bar:
    > yip;
    > break;
    > otherwise
    > yap;
    > break;
    >}


    Close. I did not have any break statements, because I was
    returning out of each case. It is the main part of a routine for
    determining the number of days in a month.

    >'otherwise' is a statement that'd belong to the list of statements
    >associated with 'case bar:' and be unreachable code because placed after
    >a break.


    I suppose this accounts for no error message being generated.

    [snip]

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Dec 15, 2011
    #5
  6. Gene Wirchenko

    Ross McKay Guest

    On Wed, 14 Dec 2011 18:09:21 -0800, Gene Wirchenko wrote:

    > An error message letting me know that I had botched would have
    >pleased me.


    --- start: var/www/html/junk/junk.js ---
    var a = 'bcdefg', b;
    switch (a) {
    case 'h':
    b = 1;
    break;

    otherwise:
    b = 2;
    break;
    }
    --- end: var/www/html/junk/junk.js ---

    jsl -conf ~/jsl.conf -process junk.js (in directory: /var/www/html/junk)
    JavaScript Lint 0.3.0 (JavaScript-C 1.5 2004-09-24)
    Developed by Matthias Miller (http://www.JavaScriptLint.com)
    junk.js
    /var/www/html/junk/junk.js(7): lint warning: use of label
    otherwise:
    ..................^
    /var/www/html/junk/junk.js(10): lint warning: missing default case in
    switch statement
    }
    ^
    0 error(s), 2 warning(s)
    Compilation failed.
    --
    Ross McKay, Toronto, NSW Australia
    "Let the laddie play wi the knife - he'll learn"
    - The Wee Book of Calvin
     
    Ross McKay, Dec 15, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Duffy

    Python switch for syntax checking

    Jeff Duffy, Nov 19, 2004, in forum: Python
    Replies:
    6
    Views:
    562
    Scott David Daniels
    Nov 19, 2004
  2. Christopher Benson-Manica

    switch syntax and style

    Christopher Benson-Manica, Jul 20, 2004, in forum: C Programming
    Replies:
    6
    Views:
    349
    Christopher Benson-Manica
    Jul 20, 2004
  3. Michael Mair
    Replies:
    1
    Views:
    411
    S.Tobias
    Jun 14, 2005
  4. Guybrush Threepwood

    syntax error in switch statement

    Guybrush Threepwood, Jun 8, 2007, in forum: C Programming
    Replies:
    8
    Views:
    390
    Christopher Benson-Manica
    Jun 8, 2007
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    109
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page