Skip to content

Instantly share code, notes, and snippets.

@flipjs
Last active August 29, 2015 14:04
Show Gist options
  • Save flipjs/b969fb7b659de5d04e95 to your computer and use it in GitHub Desktop.
Save flipjs/b969fb7b659de5d04e95 to your computer and use it in GitHub Desktop.
Switch statement alternative using object
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