Why in ES6 get and set?

Why in the ES6 get and set, if the property still has direct access(and read and write)??

class Dream {
 constructor() {
 this._name = 'Roman';
 get name() {
 return this._name;
 set name(value) {
 this._name = value;
console.log(this.name); // Roman
console.log(this._name);// Roman
July 8th 19 at 16:32
3 answers
July 8th 19 at 16:34
You can do properties that are calculated on the fly (example suburbanity):
get fullName() { return `${this.name} ${this.surname}`; },
set fullName(value) { [this.name, the this.surname] = value.split(' '); },

You can do automatic persistence (not a good idea, to be honest, but sometimes necessary):
set someProp(value) { localStorage.set('someProp', value) }

You can tie a temporary logging to capture calls to the property.

Vue.js uses accessory to implement reactivity.

In fact, applications not so much, but sometimes you need.
July 8th 19 at 16:36
Because accessors in JS is designed to ask some logic to the getter or setter. Thus, for example, to implement a counter to how many times some property was changed, etc.
And with the help of them you can implement such a thing as:

var Test;
(function() {
 var super_priv = 1;
 Test = class {
 get priv() {
 return super_priv;
 set priv(val) {
 super_priv = val;
 return priv;
What is special about this example? There is no direct access to super_priv? Only through accessors? And thanks for this need a LIFE and GET\SET ? - Lauren.Bay commented on July 8th 19 at 16:39
: the feature of this example is that it is a reworked example of the author. - cesar.Muller commented on July 8th 19 at 16:42
July 8th 19 at 16:38
Here, and good properties is primarily intended not to prohibit, and "control" the process of setting and retrieving a property value, a full ban is impossible in any language.
properties are not primarily the ban, and "control" the process of setting and retrieving property values

You have recursion. Properties store state accessors (get/set) isolate knowledge about the condition, a kind of method to achieve encapsulation and not break backward compatibility. Is very convenient. - Lauren.Bay commented on July 8th 19 at 16:41
: And you have but thoughts from the book, in your own words try, even in a book does not rely, and have enough to argue with my point of view is generally no such thing as "isolation", I have this "isolation" open can, of course, understand that this is not desirable, but there are times when you work with someone else's pre-built library, and you have to refer to private members. - cesar.Muller commented on July 8th 19 at 16:44
: "thoughts from the book"?) So you think that adequately formulate the idea can only be on the books? You will be surprised but I don't so much read them) - Allene_Crona78 commented on July 8th 19 at 16:47

Find more questions by tags JavaScript