Getters and Setters


Javascript

Updated Jul 7th, 2021

Why you need these still alludes me, I’m thinking it must be a performance things because I’m getting by instantiating objects with complex properties just fine without them.

Debating on whether I should calculate nrm and cprm with getters or just keep doing what I’ve been doing

I saw and example of get CPRM() {returns this.calcCPRM()} used in combo with calcCPRM() {return this.miles * this.price}

Need to dig a little deeper on this.

I enjoyed this article.

Properties are static key-value pairs, while methods are functions specific to the object. Maybe they help keep your data static instead of doing calcs in the value definition?

Also might be more about changing values using external data. This would make sense for nrm or cprm when the setting change the property updates.

Because getters make use of the “this” keyword often in combo with “return”

A couple syntax ways to use getters and setters. The biggest advantage of using the get and set keywords is that you can keep data properties private.

You don’t necessarily have to use getters and setters when creating a JavaScript object, but they can be helpful in many cases. The most common use cases are (1) securing access to data properties and (2) adding extra logic to properties before getting or setting their values.


Top