I was reading about data privacy in JavaScript and how to achieve that in ES6 Class. I notice this
class Person {
constructor(name) {
let _name = name
this.setName = function(name) { _name = name; }
this.getName = function() { return _name; }
}
}
then we cannot access _name like
const p1 = new Person('bob');
p1._name = 'John';
because _name is not part of the properties of the object. It seems like it can achieve data privacy. But I was wondering if this is a good practice.