How is this code correct "tempObj['num']"?


#1

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/quizzes/learn-javascript-objects-objects-quiz

In this quiz, I struggled to understand this question, but finally cannot.

let tempObj = {
  _num: 22,
  get num() {
    return this._num;
  }
}

I think that tempObj[‘num’] searches ‘num’ key. So, doesn’t tempObj[‘num’] return error ?
I researched how tempObj[’_num’] works, then this code returns 22.

Could someone teach me how tempObj[‘num’] work ?


#2
 > tempObj.num()
 X Uncaught TypeError: tempObj.num is not a function

Behind the scenes the getter is called as if we wrote, tempObj.num.


#3

Thank you so much, I’m glad to hear that the getter is called when we write tempObj[‘num’] as if wrote tempObj.num.