Last active
August 29, 2015 14:04
-
-
Save flipjs/b969fb7b659de5d04e95 to your computer and use it in GitHub Desktop.
Switch statement alternative using object
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
var classifyShape = function(shape) { | |
var isTriangle = function() { | |
console.log('A triangle is a polygon with 3 sides.') | |
} | |
var isQuadrangle = function() { | |
console.log('A quadrangle is a polygon with 4 sides.') | |
} | |
var isPentagon = function() { | |
console.log('A pentagon is a polygon with 5 sides.') | |
} | |
var isUnknown = function() { | |
console.log('Unknown shape.') | |
} | |
var shapes = { | |
'isosceles': isTriangle, | |
'rectangle': isQuadrangle, | |
'square': isQuadrangle, | |
'unknown': isUnknown | |
} | |
return (shapes[shape] || shapes['unknown'])() | |
} | |
classifyShape('square') | |
classifyShape('isosceles') | |
classifyShape('qwerty') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment