Scope of this Keyword

Welcome to the Get Help category!
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

let menu = {

    _courses: {

      appetizers: [],

      mains: [],

      desserts: [],

    },
...

    get courses() {

      return {

        appetizers: this.appetizers,

        mains: this.mains,

        desserts: this.desserts

      };
    }

How is it possible that this can refer to the object outside of the current object being returned?

Do you mean how inside your courses() getter, you’re able to access the appetizers array from the _courses object by just using this.appetizers? I think it might be because of what you chose to include in the snippet.

If it is, then the answer is that it’s doing it indirectly. this.appetizers is referring to the appetizers getter that you had to write in the project too. That appetizers getter is then returning the appetizers array from the _courses object.

So in that example, get courses() accesses the get appetizers() using this.appetizers. get appetizers in turn returns the _courses.appetizers array.

If it’s not what you mean and you’re wondering how this.appetizers is able to refer to the object’s appetizers getter at all, then let me know.

This particular project can be confusing because you’re creating getters and setters for the object, but will ultimately not even use them when you get into building a meal.

1 Like

Yes, I’m wondering how this can refer to the getter at all. It seems to somehow be bypassing the object it’s in.