Private variables in class

class Stack { #count = 0; #items = 0; // stack method } We can declare private properties by prefixing them with a hash sign (#). This behavior is very similar to private properties in WeakMap . So in the near future, we will hopefully be able to use private class properties without using special tricks or sacrificing code readability.







insert image description here


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

Guess you like

Origin blog.csdn.net/weixin_43553701/article/details/126656155