Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Greg Schudel
4,090 PointsSyntax Question about grabbing Keys of an object
I'm taking a course where I am being asked this question:
If I had the key of "created at" how would I access the value of that property on an object?
The correct answer is blogPost["created at"];
This makes ZERO sense to me when I just made a project that called the key of an using this:
QuizUI.displayNext();
the complete object looked like this:
var QuizUI = {
// I am compleletly lost at this syntax, NEVER seen it before in my life,
//is this an array with a function as the key of each value?
displayNext: function(){
if (quiz.hasEnded()) {
this.displayScore();
}else {
// this updates all parts of the UI
this.displayQuestion();
this.displayChoices();
this.displayProgress();
}
},
displayChoices: function(){
this.populateIdWithHTML("question", quiz.getCurrentQuesiton().text);
},
displayChoices: function(){
var choices = quiz.getCurrentQuestion().choices;
for(var i = 0; i < choices.length; i++){
this.populateIdWithHTML("choice" + i, choices[i]);
this.guessHandler("guess" + i, choices[i]);
}
},
displayScore: function(){
var gameOverHTML = "<h1>Game Over</h1>";
gameOverHTML += "<h2> Your score is: " + quiz.score + "</h2>";
this.populateIdWithHTML("quiz", gameOverHTML);
},
populateIdWithHTML: function(id, next) {
var element = document.getElementById(id);
element.innerHTML = text;
},
guessHandler: function(id, guess){
var button = document.getElementById(id);
button.onclick = function(){
quiz.guess(guess);
QuizUI.displayNext();
}
},
displayProgress: function(){
var getCurrentQuestion = quiz.currentQuestionIndex + 1;
this.populateIdWithHTML("progress", "Question" + currenyQuestionNumber + " of " + quiz.questions.length);
}
}
what am I not understanding about this syntax?
2 Answers
Steven Parker
243,200 PointsThe word "called" is a clue indicating the difference between a property and a method. In your example you are calling a method of an object, but the original question was about accessing the value of a property.
Also the "dot" notation you used to get to your method would also normally be used with properties, but not if the key contains a space. That's why the bracket notation is necessary for this particular property.
Greg Schudel
4,090 PointsThank you thank you thank you thank you!!!!
Greg Schudel
4,090 PointsGreg Schudel
4,090 Pointsbut wouldn't I use this one? blogPost"created at";
Isn't this correct syntax or am I confusing that with something else?
Steven Parker
243,200 PointsSteven Parker
243,200 PointsYou still need the brackets. In JavaScript, it's never correct to place a literal string next to another identifier.