Skip to content

Instantly share code, notes, and snippets.

@krawaller
Created November 1, 2017 12:37
Show Gist options
  • Save krawaller/4f635890884fcdf944b2071a37381521 to your computer and use it in GitHub Desktop.
Save krawaller/4f635890884fcdf944b2071a37381521 to your computer and use it in GitHub Desktop.
object lookup examples
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