Method Calls

Hello All,

for this section, all my steps are green, but the output is

ReferenceError: name is not defined
at Surgeon.get name [as name]

i don’t know which part gone wrong, and also for below, i don’t know when to add () and when i don’t, coz in the tutorial there is no (). But i feel like here i should. very confused…



Hi, in order to run / invoke a function we use ().

Getters and Setters don’t work like functions.

Your code kind of breaks at the name getter.

You can think of a getter as a “shield” for the variable _name. It listens for a name call, how does the getter know what to listen for? From that: get name() {. We don’t use getters for variable assignments, that’s what setters are for. Getters just return whatever is stored in the variable they’re “shielding”. Try comparing your working getter with the broken getter and look for a difference.

1 Like

hmmm i guess i am not very clear with getter and setter still…So what should i change here? still a bit confused…

Get indicates retrieval. This is an assignment.

I am sorry…what does “Get indicates retrieval” mean? I am bit lost here…:flushed:

Getters and Setters protect your object from getting torn apart by whatever code the object get’s put into / accessed from, thus helping the “outside code” to not break itself.

Imagine you’ve got an object with all sorts of data(data you want to be mutable and data that you don’t want to change).

Then, you want to use that object in some code, of course. How do you have that object interact with the rest of your code without breaking the object itself?

Example: the outside code tries to change something in your object, you don’t want it to change because you know changing that part would render your object useless.

Getters, are helpers that let the rest of the code see what’s stored inside the object(“retrieval”), they get the data values from the variables inside and return them to the outside. Setters are similar, they set variables you allow to be changed, if there is no setter inside the object for a value, then the code outside of the object (functions for example) won’t be able to change it. That helps keeping your Object functional.

1 Like

Thank you very much! It is very elaborated explanation, i think i get it now. But need more practice to really comprehend it. So, here, what can i do to make it work? (i don’t understand why the steps show green why i don’t get an outcome).

1 Like

Getters are not treated as methods.

is the expected syntax. That brings us to the getter,

  get name() {
    return this._name = name;

is not the correct syntax. We do not make an assignment in a getter as there is no argument. Only return the aliased attribute value.

return this._name;

Thanks!!! For your patience…I know it is pain to explain such a simple question. But it helps a lot!

1 Like