JavaScript: Trouble with switch Syntax

G

Gene Wirchenko

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
 
T

Tim Streater

Jake Jarvis said:
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.
 
A

Aaron Sawyer

Gene Wirchenko said:
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!
 
G

Gene Wirchenko

On Wed, 14 Dec 2011 18:58:55 -0400, "Aaron Sawyer"

[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
 
G

Gene Wirchenko

On Wed, 14 Dec 2011 23:32:47 +0100, Jake Jarvis

[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
 
R

Ross McKay

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top