0

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

2
  • 1
    It's also readonly, if you try and assign to it you'll get an error in strict mode (or it just won't be assigned in non-strict code) Commented Apr 8, 2024 at 9:37
  • Thanks. I tried it out. And this IS a pretty big difference Commented Apr 8, 2024 at 10:28

3 Answers 3

4

A Function is what ever you want it to be, some parameters, some code, and some returning or none of these.

But a get Function, is strict to no parameters, with return value. it's a computed property, more like a property.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is actually helpful.
I want to add a little something since I am still on this subject: An object is a collection of properties, and a property is an association between a name (or key) and a value. (MDN Web Docs). So GET indeed allows to create a computed value as opposed to STORED VALUE.
0

The syntax is the main difference - and an important difference. Using a getter is indistinguishable from using a normal data property, they have exactly the same property access syntax.

This encapsulation of the object implementations in an interface allows decoupling the implementation of the object from its usage in other code, permitting seamless refactoring where you change back and forth between data properties and accessor properties. If you were using a function (i.e. getter and setter methods), you would always have to use calls instead of accessing the property by its name, leading to much boilerplate when having to wrap every data property access in an unnecessary method.

Comments

-2

From MDN explanation:

While using the get keyword and Object.defineProperty() have similar results, there is a subtle difference between the two when used on classes.

When using get the property will be defined on the instance's prototype, while using Object.defineProperty() the property will be defined on the instance it is applied to.

Get keyword is better used in classes.

4 Comments

Get keyword is better used in classes. why? That seems to be an opinion :p
Yeah, but I was asking slightly different thing that had nothing to do with Object.defineProperty()
@JaromandaX Based on citation above. Outside classes both ways do almost the same. Difference becomes more obvious when used inside class. It is not you cannot use it outside class.
There is not really a difference. Getters defined in object literals and getters defined in class prototypes work exactly the same way. The difference is wrt Object.defineProperty, which doesn't have anything to do with the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.