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?
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 ().
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.
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)… :-))
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.
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.
I’m still a bit confuse about the purpose of get function name ?
Is it just to avoid to put two parenthesis ?
If i removed the get i have the same results when i log into the console:
const robot = {
_model: ‘1E78V2’,
_energyLevel: 100, //i removed get
testLevel(){
if(typeof this._energyLevel === ‘number’){
return My current energy level is ${this._energyLevel}
;
} else { return System malfunction: cannot retrieve energy level
;
}
}
};
console.log(robot.testLevel()); //i added ( )
So why do we actually use get?
The get
syntax binds an object property to a function that will be called when that property is looked up, according to MDN.
So Javascript does some “magic” here. The getter creates a property, and binds the property to a method which will be called when we access the property
this enforces that the energy level is a number, or an error message is displayed.
Let me get this straight, getters are used to access properties in a certain object?
Now, the difference between using getters and simply accessing properties is that getters allow us to ‘safely’ access properties. So if a property was equal to something, we could return it, but if it was equal to something else, we could return something else?
Now, the only difference between getters and simple methods is that we can use them along with setters to change the value of a property or many properties at once
Getters make it possible to access backing variables using conventional syntax. There’s nothing safe about it, just a better practice for all collaborators to follow.
We can build logic into a getter but in my view it is better to simply return the value in its native form (type) with no other appendages or modifications. If we want to dress it up we can use another method that accesses the getter.
Only the getter should ever be used to access the variable for polling. Only the setter should be used to access the variable for writing. All other methods can call the getter/setter. When properly implemented it’s a seamless affair.
While we can write logic in a setter, such as type or value validation, we may wish to keep things simple. Add more methods for increased complexity.
Lastly, bear in mind that getters, get, and setters set. The former returns the value of the backing property, the latter sets the value without any return.
So what’s the difference between using a getter (if you only use it to access the property and return it) and directly accessing the property?
No real difference other than getters/setters fall more in line with OOP than the traditional means of accessing properties. When properly implemented it is very easy for everyone involved to go along with.
Great explanation, thank you!
Getters and setters are accessor properties used to get or set the data. Just like the length property of arrays, calling getter does not require parathesis; for example, when we use arr.length, it returns the number of elements in array ‘arr’ no matter how many elements we add to it.
Every object has a length
property, though for some it is not updated, particularly object literals (afaik) which show it as undefined
. When we mutate an array (add, remove, concatenate) JS automatically updates the length
property.
Getters and setters have properties in the background that correspond to the backing variables in the object declaration. Any change to those properties will be reflected in the main properties assuming they are focused on the backing variables.