I have hard time understanding why there are two different ways to perform an absolutely identical operation in JavaScript objects.
Here I've got an object which contains an array in its "log" property. And the "latest" property of obj stores the last item of the array which is done with the help of a function.
const obj = {
log: ['a', 'b', 'c'],
latest: function () {
return this.log[this.log.length - 1];
},
};
console.log(obj.latest());
But then later in the tutorial the keyword Get is introduced and its existence kinda confuses me because it does the exactly same thing here:
const obj = {
log: ['a', 'b', 'c'],
get latest() {
return this.log[this.log.length - 1];
},
};
console.log(obj.latest);
They explained the get keyword provides simpler syntax by allowing to call "latest" like a regular property (obj.latest) as opposed to obj.latest() which is a method.
But is there anything more to it other than simplified syntax? Thanks