这个问题在这里已有答案:

看下面的代码:

class Animal() {
  constructor(type, name) {
    this._type = type;
    this._name = name;
  }

  get type() {
    return this._type;
  }

  get name() {
    return this._name;
  }
}

如果我要跑:

var Dog = new Animal("Dog", "Rover"); 
console.log(Dog._name);

控制台将返回我的狗的名字,而不需要使用我上面写的`get name(){} . 如果我可以直接访问我的对象属性,Javascript 6中的getter和setter有什么意义?当私有 property 没有私有化时,为什么强调私有 property 的约定?如何使用当前的ecmascript-6标准在Javascript中的类中将我的属性设为私有?