Last active
December 4, 2015 18:45
-
-
Save adamterlson/d22563f2ea7b93834bdb to your computer and use it in GitHub Desktop.
English?
This file contains hidden or 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
let user = { | |
teacherId: 1, | |
first: 'Bob', | |
last: 'Smith', | |
room: { | |
floor: 2, | |
number: 40 | |
} | |
}; | |
// Switchish | |
if (user.teacherId) { | |
// is it a teacher? | |
// More switchish | |
if (user.room) { | |
if (user.room.floor === 1) { | |
// Do stuff | |
} else if (user.room.floor ===2) { | |
// Do other stuff | |
} | |
} | |
} else if (user.studentId) { | |
// is it a student? | |
} else { ... } | |
// The point: Once I know that this object is not a student but a teacher, I go on to | |
// make more specific checks about the state. At no point would my initial "narrowing down" | |
// become invalid where I would want to "fall through" the teacher bits to see if it's a student. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment