Why don't we need parenthesis for getters?

Please could someone explain why the end of this lesson the answer is:

console.log(robot.energyLevel)

and not

console.log(robot.energyLevel())

and when to know when to put the brackets in please?

Hi, I am just learning about getters and setters myself and without seeing your code I can’t be sure,
but I believe that when calling getter and setter methods, the function is called without ().

5 Likes

This makes sense actually I think they did mention that somewhere and I forgot! Thank you for your help

get attribute () {
    return this._attribute;
}

We write a get call the way we would access an object property, normally.

console.log(object.attribute);

We know there is no literal attribute by that name, but a backing variable with an underscore, which value is returned by the getter. We never invoke the getter method as we would a typical method, since it is self-invoking and accesses the actual attribute and returns it.

Likewise, we never invoke a setter, but call it like we would be making an assignment.

object.attribute = newValue;

The mechanics behind the scene are such that we cannot access (get) a variable while it is being set. It’s a bit complicated to fully understand, let alone explain, but warrants further study as you get more immersed in the finer details. Add this to your reading list.

18 Likes

Hi,

in my opinion, using getters w/o brackets is quite self-explanatory, as we can think we are asking for a property, not calling a function/method. I find little less ‘natural’ to use setter w/o them, I would even use robot.energyLevel(150)… :-))

2 Likes

Just as we are writing getters to emulate property polling, we write setters to emulate property value assignment.

Pseudo-code…

    obj._prop: value;

    get prop () {

    }
    set prop (value) {

    }

console.log(obj.prop);    // value

obj.prop = newValue;

console.log(obj.prop);    // newValue

The reason we cannot use function arguments is because setters are not functions (and neither are getters).

Clearly it comes down to implementation and what advantage it gives the programmers. We can choose to use the typical object model (ES5), or the ES6 model which has gained wide support. To look for mechanical reasons why the one is better than the others is pointless.

2 Likes

This video explains the best this lesson: https://www.youtube.com/watch?v=bl98dm7vJt0

8 Likes

Hi
It seems to me it is one of JavaScript inconcistency. After all, a getter is a METHOD

Jaime Fernandez

Thanks a ton! This will make it much easier for me to understand, Mosh Hamedani is da man!

If getters and setters are not a function, does that mean they don’t have parameters?

Getters and setters are special methods with a little bit going on in the background that we don’t see or control. The getter does not have a parameter, and the setter can only take a single parameter that must be there.

Note that these special methods have a distinct purpose which you are wise to investigate further so these methods can be set apart from normal methods (of an object) or functions.

2 Likes