FAQ: Advanced Objects - Setters


#1

This community-built FAQ covers the “Setters” exercise from the lesson “Advanced Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Setters

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

The last task of the Setters section reads:
To check that the setter method worked, console.log() robot.numOfSensors .

This doesn’t make sense to me. Shouldn’t we be checking that _numOfSensors was updated with the setter?
also, if I am wrong, is robot.numOfSensors calling the getter or the setter? Since both the getter and setter have the same name, I’m not sure what is being called here.


#3

get and set are special keywords that behave like properties, the name of which is the same for both because they both relate to the same backing variable (the one with the underscore prefix). We cannot call them directly because they are engaged in the background.

Above we must assume that the value has already been set, and we’re only polling the variable for its current value. When we write,

console.log(robot.property);

we are accessing the getter, which then returns the backing variable value the same way a typical object would, and when we write,

robot.property = "some value";

we are accessing the setter, but it depends upon the getter for some special binding which I don’t really understand well enough to explain but if you can handle dry technical reading, I recommend digging deeper into the relationship, and why setters depend upon getters in the first place. We can’t have set without get for the variable in question.


#4

Yes! I get it now. Thank you @mtf!


#5

You’re welcome.

Aside

One thing you don’t want to do is create a circular reference. Consider the following code…

class Robot {
    constructor(foo, bar) {
        this._foo = foo;
        this._bar = bar;
    }
    get foo() {
        return this._foo;
    }
    set foo(x) {
        this.foo = x;
    }
    get bar() {
        return this._bar;
    }
    set bar(x) {
        this.bar = x;
    }
}

Notice how the setters are calling the getter, not the backing variable? That conflicts with what’s already going on in the background and pushes all the calls to a stack. The browser steps in and stops it eventually.

Uncaught RangeError: Maximum call stack size exceeded

That’s what we would call a fatal error. Keep this in mind going forward so you never make this mistake.

Bottom line, we cannot set and get a variable at the same time. Only set or get.


#6

Good to know . Thanks!