Created
August 7, 2015 20:46
-
-
Save chuck0523/ae3c60c99751c6b09583 to your computer and use it in GitHub Desktop.
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 obj = { | |
mon : 0, | |
tue : 1, | |
wed : 2, | |
thu : undefined, | |
time : { | |
morning : "5-11", | |
night : "19-22" | |
} | |
} | |
// ||を使ってデフォルト値 | |
var daytime = obj.time.daytime; | |
console.log(daytime); //undefined | |
var daytime = obj.time.daytime || "11-17"; | |
console.log(daytime); //11-17 | |
// &&を使ってTypeErrorを防ぐ | |
console.log(obj.fri); //undefined | |
console.log(obj.fri.detail); //TypeError !! | |
console.log(obj.fri && obj.fri.detail); //undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment