Created
November 1, 2017 12:37
-
-
Save krawaller/4f635890884fcdf944b2071a37381521 to your computer and use it in GitHub Desktop.
object lookup examples
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 me = { | |
name: "David Waller", | |
nickName: "Davve", | |
prop: "kopp" | |
}; | |
var formalSituation = true; | |
var prop; | |
// ------------ LÖSNING 1 | |
if (formalSituation){ | |
prop = "name"; | |
} else { | |
prop = "nickName"; | |
} | |
console.log("Hello my name is", me[prop]); | |
// ---------- LÖSNING 1.5 | |
prop = formalSituation ? "name" : "nickName"; | |
console.log("Hello my name is", me[prop]); | |
// ---------- LÖSNING 2 | |
if (formalSituation){ | |
console.log("Hello my name is", me.name); | |
} else { | |
console.log("Hello my name is", me.nickName); | |
} | |
// ---------- LÖSNING 3 | |
console.log("Hello my name is", me[formalSituation ? "name" : "nickName"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment