Created
December 2, 2014 21:30
-
-
Save devdays/701eeb8fba6e5ef0c5bb to your computer and use it in GitHub Desktop.
Object JavaScript - avoid property access errors
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
// Here’s a verbose technique: | |
var motherName = undefined; | |
if(spot) { | |
if (spot.mother) motherName = spot.mother.name; | |
} | |
// And here is a more concise way. | |
var motherName = spot && spot.mother && spot.mother.name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment