Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are `getter` and `setter` necessary in JavaScript?

Tags:

class Employee {      constructor(name, age) {         this._name = name;         this.age = age;     }      doWork() {         return `${this._name} is working`;     }      get name() {         return this._name.toUpperCase();     }      set name(newName){         if(newName){              this._name = newName;         }     } }  let man = new Employee('A', 10); console.log(man.name, man.age); man.name = 'B'; man.age = 20; console.log(man.name, man.age); 

Here is my code. I created a getter and setter for _name member. I did not create a getter and setter for age.

But both can update these two fields like this man.name = 'B';man.age = 20;

So I am confused, are getter and setter necessary in JavaScript?

like image 302
BlackMamba Avatar asked Jan 15 '16 05:01

BlackMamba


People also ask

Do I need getters and setters in JavaScript?

Conclusion. 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.

Do you need setters and getters?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What is the point of getters and setters in JavaScript?

Now, the key is the use Getter and Setter functions to communicate the code's intent. Once you get this set of intent down, code written with Getter and Setter functions will become easier to parse. Here's an example where we get a value, set a value, and run a process – all done with functions.


1 Answers

Are getter and setter necessary in JavaScript?

Necessary is a bit of an undefined word. Any problem can be solved without a getter and setter (usually by changing the interface to methods instead of direct property access), just like any problem can be solved without a for loop (by substituting a while loop and manually maintained loop variables), but both are useful features to have in a programming language because when they are a match for a use case, then they are the cleaner and more convenient way to program. Thus, they are "useful" and "helpful".

A getter or setter can be very useful at times, but they only need to be used when their specific functionality is required - otherwise plain property access without a getter or setter can be just fine.

A getter has use when you want to run some code every time a property is requested. In your example, the getter always returns an uppercase version of the name regardless of what the case of the name is, while reserving the actual case for internal use.

A setter has use when you want to run some code every time a property is set. In your case, it prevents the setting of a falsey name. You can't implement either of those features without a getter/setter.

Direct property access is a perfectly fine way to do things when you don't need getter or setter special logic.

It's also possible that getters and setters may get and set some property that is not publicly available (so not available at all via a normal property access), either because it's stored somewhere differently (not as a property on this object) or stored privately or even that it's stored else such as in some hardware device. In these cases, the getter and setter simulate the value being in a property when it actually isn't. So, they simplify the interface while allowing the actual value to be stored or retrieved from anywhere.

like image 108
jfriend00 Avatar answered Sep 25 '22 19:09

jfriend00