I'm playing around with ES6 Class
and i'm wondering what the point is of using the get
or set
method for properties on that class. They seem pointless b/c you can set any arbitrary property on the class anyways.
For example with this class
class Group {
constructor() {}
set name(newName) {
this._name = newName;
}
get name() {
return this._name;
}
print() {
console.log('this._name = ', this._name);
console.log('this.name = ', this.name);
}
}
I get these results
var group = new Group();
group._name = 'foo';
console.log('_name = ', group._name); => _name = foo
console.log('name = ', group.name); => name = foo
group.print(); => this._name = foo
=> this.name = foo
var group2 = new Group();
group2.name = 'bar';
console.log('_name = ', group2._name); => _name = bar
console.log('name = ', group2.name); => name = bar
group2.print(); => this._name = bar
=> this.name = bar
With the results of this example, it seems that by using the new set
and get
method just adds unnecessary bloat to my class.