Question on a Quiz Question


I’m not sure if I did something wrong but shouldn’t the answer be the 1st one? At the bottom it says tempObj.num is correct.

The description at the bottom is accurate. There is a getter named num, which does not get called like a traditional method. You can access it like other properties of the object.

If #1 didn’t work to access the value, then #3 would also not work, but they both do.

Consider the following:

const  test = {
  _message: 'hello, world',
  get message() {
    return this._message;
  },
  set message(msg) {
    this._message = msg;
  },
  regularMethod() {
    return this.message;
  }
};

console.log(test.message); // hello, world

test.message = 'testing';

console.log(test.message); // testing
console.log(test['message']); // testing
console.log(test._message); // testing
console.log(test['_message']); // testing

test['message'] = 'turtles';

console.log(test.message); // turtles

console.log(test.regularMethod()); // turtles

console.log(test.message()); // TypeError: test.message is not a function

Using getters and setters allows you to use standard notation to access the values rather than call object methods directly. Notice that from within the regularMethod(), we can use the getter of the object as well.

2 Likes

Oh sorry, I’m such an idiot. I somehow read the question too fast and didn’t see the ‘not’. Thank you for responding anyhow.

No, you’re not. You’re learning, and making mistakes is part of that. Positive self-talk is part of a growth mindset, which will really help you along the way.

I also know that you didn’t ask for this interjection from me; I saw that you said this about yourself and I just couldn’t help but step in and share this. How we speak to ourselves can have a big impact on our success, and I want you to be successful :slight_smile:

1 Like