Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JavaScript: Trouble with switch Syntax

Reply
Thread Tools

JavaScript: Trouble with switch Syntax

 
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-14-2011
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
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      12-14-2011
In article <(E-Mail Removed)-berlin.de>,
Jake Jarvis <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Aaron Sawyer
Guest
Posts: n/a
 
      12-14-2011

"Gene Wirchenko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/libr...=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-...-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!


 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-15-2011
On Wed, 14 Dec 2011 18:58:55 -0400, "Aaron Sawyer"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-15-2011
On Wed, 14 Dec 2011 23:32:47 +0100, Jake Jarvis
<(E-Mail Removed)> 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
 
Reply With Quote
 
Ross McKay
Guest
Posts: n/a
 
      12-15-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
Switch/Switch problem fibre gigbit ethernet hartmut.albers@gmail.com Cisco 2 09-06-2005 04:59 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
bridge / layer 2 switch / layer 3 switch Joel M. Baldwin Cisco 2 11-06-2003 11:19 PM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments