クラス内のプライベート変数

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()

おすすめ

転載: blog.csdn.net/weixin_43553701/article/details/126656155