简体   繁体   中英

what is the advantage of setters in javaScript

So I have been learning JavaScript since awhile ago and I really cant get the advantages of using setters.
Like yes getters seems useful to avoid unnecessary replacement of value of a variable though as for setters, it says that it can be useful to check if new data is valid before setting a property however we can do that without even using a setter
For instance,

    const person = {
      height: 150,
      set newHeight(num){
        if(typeof(num)=='number' && num>=0)
        {
          this.height=num
        }
        else
        {
          console.log('error');
        }
      }
    };

    person.newHeight=200
    console.log(person.height) //output:200

so this is using setter,
while if we were not to use setter then

const person = {
  height: 150,
  newHeight(num){
    if(typeof(num)=='number' && num>0)
    {
      this.height=num
    }
    else
    {
      console.log('error');
    }
  }
};

person.newHeight(200)
console.log(person.height) //output:200

so whats really the difference between using setters and not using one
Sorry if i seem to have gotten something wrong
Thanks in advance!!!

Getters and setters originate from the idea of encapsulation, which is a much stronger concept in languages like Java. In Java the following code is highly discouraged:

myObj.somePublicVar = newVal;

Whereas this is common practice in Javascript. In Java, the convention is to create a getter and setter for every variable you intend to make public, and keep the variable themselves declared as private . So you would have:

private int myVar;
public int getMyVar() {
  return myVar;
}
public void setMyVar(int val) {
  this.myVar = val;
}

In theory, this is helpful in preventing unintended or unwanted modification of an object's internal states. For example, you might wish to only expose the getter of said states without allowing the ability to set them. This way, other programmers using your code as library will less likely break your code by unknowingly messing up some important states. Also for example, you might wish to validate the new value before actually setting it, so you would have something like:

public void setScore(int val) {
  if (val < 0) this.score = 0;
  else if (val > 100) this.score = 100;
  else this.score = val;
}

In practice though, this strict enforcement of convention often causes more headaches than benefits. You often see Java classes with more than 50% of lines dedicated to various getters and setters, which slows down development.


Javascript added support for getters and setters so that if developers want stricter control, they can do so . However, unless you are developing a library, or you are a part of a big cooperative project, I doubt if the benefits will be of any practical value. So TLDR; if you are just a casual developer, or if you are working on small projects only, don't bother - it will cause more trouble than it will do good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM