class Stack { #count = 0; #items = 0; // stack メソッド}プライベート プロパティを宣言するには、ハッシュ記号 (#) をプレフィックスとして付けます。この動作は、 WeakMap のプライベート プロパティに非常に似ています。したがって、近い将来、特別なトリックを使用したり、コードの可読性を犠牲にしたりすることなく、プライベート クラスのプロパティを使用できるようになることを期待しています。
class Persion {
#name = '李大玄'
sex = '男'
constructor() {
this.age = 19
}
getName() {
return this.#name;
}
getSex() {
return this.sex;
}
}
const persion = new Persion();
const name = persion.getName()
const sex = persion.getSex()
console.log('name: %s', name);
console.log('name: %s', sex);
class One extends Persion {
getName() {
return this.#name;
}
getSex() {
return this.sex;
}
}
const one = new One();
const onename = one.getName()