Created
April 25, 2014 14:53
-
-
Save t-uda/11292346 to your computer and use it in GitHub Desktop.
switch(true) イディオム考察 ref: http://qiita.com/t_uda/items/1969e09a970d71e4cfd6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
もし条件 condition が成り立つならば hoge する. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expr の評価結果によって場合分けする. | |
A の場合は,hoge する. | |
B の場合は,fuga する. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
true の評価結果(??)によって場合分けする. | |
x < 0 の場合は,hoge する. | |
x === 0 の場合は,fuga する. | |
x > 0 の場合は,piyo する. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hoge(x) { | |
if (x < 0) { | |
console.log(x + " は自然数ではありません."); | |
} else if (x === 0) { | |
console.log("ここでは 0 は自然数です."); | |
} else if (x > 0) { | |
console.log(x + " は正の数です."); | |
} else { | |
console.log(x + " は数ではないようです."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hoge(x) { | |
switch (true) { | |
case x < 0: | |
console.log(x + " は自然数ではありません."); | |
break; | |
case x === 0: | |
console.log("ここでは 0 は自然数です."); | |
break; | |
case x > 0: | |
console.log(x + " は正の数です."); | |
break; | |
default: | |
console.log(x + " は数ではないようです."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// local scope | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment