Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9238 > unrolled thread

JavaScript: Trouble with switch Syntax

Started byGene Wirchenko <genew@ocis.net>
First post2011-12-14 13:23 -0800
Last post2011-12-15 18:33 +1100
Articles 10 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  JavaScript: Trouble with switch Syntax Gene Wirchenko <genew@ocis.net> - 2011-12-14 13:23 -0800
    Re: JavaScript: Trouble with switch Syntax Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-14 23:32 +0100
      Re: JavaScript: Trouble with switch Syntax Tim Streater <timstreater@greenbee.net> - 2011-12-14 22:39 +0000
      Re: JavaScript: Trouble with switch Syntax Gene Wirchenko <genew@ocis.net> - 2011-12-14 18:11 -0800
        Re: JavaScript: Trouble with switch Syntax Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-16 21:28 +0000
    Re: JavaScript: Trouble with switch Syntax "Aaron Sawyer" <aaron.sawyer@deltaware.com> - 2011-12-14 18:58 -0400
      Re: JavaScript: Trouble with switch Syntax Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 00:42 +0100
        Re: JavaScript: Trouble with switch Syntax Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 01:06 +0100
      Re: JavaScript: Trouble with switch Syntax Gene Wirchenko <genew@ocis.net> - 2011-12-14 18:09 -0800
        Re: JavaScript: Trouble with switch Syntax Ross McKay <au.org.zeta.at.rosko@invalid.invalid> - 2011-12-15 18:33 +1100

#9238 — JavaScript: Trouble with switch Syntax

FromGene Wirchenko <genew@ocis.net>
Date2011-12-14 13:23 -0800
SubjectJavaScript: Trouble with switch Syntax
Message-ID<ug4ie75gitc6gc9rsse94i0slcpumt1pg0@4ax.com>
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

[toc] | [next] | [standalone]


#9240

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2011-12-14 23:32 +0100
Message-ID<9ksmgfF7pkU1@mid.uni-berlin.de>
In reply to#9238
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;
   // }

}

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#9241

FromTim Streater <timstreater@greenbee.net>
Date2011-12-14 22:39 +0000
Message-ID<timstreater-9E2348.22393814122011@news.individual.net>
In reply to#9240
In article <9ksmgfF7pkU1@mid.uni-berlin.de>,
 Jake Jarvis <pig_in_shoes@yahoo.com> 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

[toc] | [prev] | [next] | [standalone]


#9254

FromGene Wirchenko <genew@ocis.net>
Date2011-12-14 18:11 -0800
Message-ID<omlie7tv013gm2b3861rlmujovp694s7cd@4ax.com>
In reply to#9240
On Wed, 14 Dec 2011 23:32:47 +0100, Jake Jarvis
<pig_in_shoes@yahoo.com> 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

[toc] | [prev] | [next] | [standalone]


#9327

FromDr J R Stockton <reply1150@merlyn.demon.co.uk>
Date2011-12-16 21:28 +0000
Message-ID<0Y0i$$HCg76OFwkx@invalid.uk.co.demon.merlyn.invalid>
In reply to#9254
In comp.lang.javascript message <omlie7tv013gm2b3861rlmujovp694s7cd@4ax.
com>, Wed, 14 Dec 2011 18:11:25, Gene Wirchenko <genew@ocis.net> posted:

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

function DiM(Y, M) { // M = 1..12
  return new Date(Y, M, 0).getDate() }

  return new Date(Date.UTC(Y, M, 0)).getUTCDate() // may be faster.

For another way, read about Zeller's Congruence; his relevant papers are
on my site.

For speed, try the codes in my  js-date3.htm#ML  - a button is provided.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#9243

From"Aaron Sawyer" <aaron.sawyer@deltaware.com>
Date2011-12-14 18:58 -0400
Message-ID<jcb9ni$s8l$1@speranza.aioe.org>
In reply to#9238
"Gene Wirchenko" <genew@ocis.net> wrote in message 
news:ug4ie75gitc6gc9rsse94i0slcpumt1pg0@4ax.com...
> 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%28v=VS.85%29.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! 

[toc] | [prev] | [next] | [standalone]


#9246

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2011-12-15 00:42 +0100
Message-ID<9ksqilF6afU1@mid.uni-berlin.de>
In reply to#9243
On 14.12.2011 23:58, Aaron Sawyer wrote:
> "Gene Wirchenko"<genew@ocis.net>  wrote in message
> news:ug4ie75gitc6gc9rsse94i0slcpumt1pg0@4ax.com...
>> 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);

   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

That is not right in this case of "reading" (identifier 'otherwise' 
alone in expression or statement), if not found, an error must occur 
(see the abstract GetValue operation in the specifications).

> (3) end of line has been encountered, therefore a semicolon must have been
> intended (!) and will be supplied by the language processor.
<snip>

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#9250

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2011-12-15 01:06 +0100
Message-ID<9ksrviFfhrU1@mid.uni-berlin.de>
In reply to#9246
On 15.12.2011 00:42, Jake Jarvis wrote:
> On 14.12.2011 23:58, Aaron Sawyer wrote:
>> "Gene Wirchenko"<genew@ocis.net> wrote in message
>> news:ug4ie75gitc6gc9rsse94i0slcpumt1pg0@4ax.com...
>>> 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:

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sorry, concentrated on just a part of the quote.

Following a 'break' in a case's statement list, the 'otherwise' 
identifier will not be acted upon at all during evaluation.

>> (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);
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> That is not right in this case of "reading" (identifier 'otherwise'
> alone in expression or statement), if not found, an error must occur
> (see the abstract GetValue operation in the specifications).
>
>> (3) end of line has been encountered, therefore a semicolon must have
>> been
>> intended (!) and will be supplied by the language processor.
> <snip>
>
<snip>

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#9253

FromGene Wirchenko <genew@ocis.net>
Date2011-12-14 18:09 -0800
Message-ID<1llie7to2t240pdm99ops4qp5q8bek13g1@4ax.com>
In reply to#9243
On Wed, 14 Dec 2011 18:58:55 -0400, "Aaron Sawyer"
<aaron.sawyer@deltaware.com> 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

[toc] | [prev] | [next] | [standalone]


#9257

FromRoss McKay <au.org.zeta.at.rosko@invalid.invalid>
Date2011-12-15 18:33 +1100
Message-ID<rh8je71riiniiqshqnt86sg8e3u4792m2j@4ax.com>
In reply to#9253
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web