Skip to content

Instantly share code, notes, and snippets.

@Kanasansoft
Created May 2, 2014 17:55
Show Gist options
  • Save Kanasansoft/fa0bcf90ef32b2dcb074 to your computer and use it in GitHub Desktop.
Save Kanasansoft/fa0bcf90ef32b2dcb074 to your computer and use it in GitHub Desktop.
JavaScriptの switch(true) で、複数のcaseにマッチする時の挙動がわからないという意見があったので書いた。
// switch(true) with fall through
var ary = [0, 1, 2, 3, 4, 5];
function compare(a, b) {
console.log("compare " + a + " with " + b);
return b - a;
}
console.log("==========");
console.log("with fall through");
ary.forEach(function(item) {
console.log("----------");
console.log("item : " + item);
switch(true) {
default : console.log("run : default" );
case compare(item, 0) >= 0: console.log("run : " + item + "/" + 0);
case compare(item, 1) >= 0: console.log("run : " + item + "/" + 1);
case compare(item, 2) >= 0: console.log("run : " + item + "/" + 2);
case compare(item, 3) >= 0: console.log("run : " + item + "/" + 3);
case compare(item, 4) >= 0: console.log("run : " + item + "/" + 4);
}
});
// result
/*
==========
with fall through
----------
item : 0
compare 0 with 0
run : 0/0
run : 0/1
run : 0/2
run : 0/3
run : 0/4
----------
item : 1
compare 1 with 0
compare 1 with 1
run : 1/1
run : 1/2
run : 1/3
run : 1/4
----------
item : 2
compare 2 with 0
compare 2 with 1
compare 2 with 2
run : 2/2
run : 2/3
run : 2/4
----------
item : 3
compare 3 with 0
compare 3 with 1
compare 3 with 2
compare 3 with 3
run : 3/3
run : 3/4
----------
item : 4
compare 4 with 0
compare 4 with 1
compare 4 with 2
compare 4 with 3
compare 4 with 4
run : 4/4
----------
item : 5
compare 5 with 0
compare 5 with 1
compare 5 with 2
compare 5 with 3
compare 5 with 4
run : default
run : 5/0
run : 5/1
run : 5/2
run : 5/3
run : 5/4
*/
// switch(true) without fall through
var ary = [0, 1, 2, 3, 4, 5];
function compare(a, b) {
console.log("compare " + a + " with " + b);
return b - a;
}
console.log("==========");
console.log("without fall through");
ary.forEach(function(item) {
console.log("----------");
console.log("item : " + item);
switch(true) {
default : console.log("run : default" ); break;
case compare(item, 0) >= 0: console.log("run : " + item + "/" + 0); break;
case compare(item, 1) >= 0: console.log("run : " + item + "/" + 1); break;
case compare(item, 2) >= 0: console.log("run : " + item + "/" + 2); break;
case compare(item, 3) >= 0: console.log("run : " + item + "/" + 3); break;
case compare(item, 4) >= 0: console.log("run : " + item + "/" + 4); break;
}
});
// result
/*
==========
without fall through
----------
item : 0
compare 0 with 0
run : 0/0
----------
item : 1
compare 1 with 0
compare 1 with 1
run : 1/1
----------
item : 2
compare 2 with 0
compare 2 with 1
compare 2 with 2
run : 2/2
----------
item : 3
compare 3 with 0
compare 3 with 1
compare 3 with 2
compare 3 with 3
run : 3/3
----------
item : 4
compare 4 with 0
compare 4 with 1
compare 4 with 2
compare 4 with 3
compare 4 with 4
run : 4/4
----------
item : 5
compare 5 with 0
compare 5 with 1
compare 5 with 2
compare 5 with 3
compare 5 with 4
run : default
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment