static
static定义的是改类自己的静态方法。
- 不静态方式是作为
构造函数的方法而非原型对象的方法 使用static定义的方法,
只能通过构造函数调用,不能使用实例调用class Person{static get(){console.log('person set fun')}set(){console.log('set')}}class Ming extends Person{}const p = new Person();const m = new Ming();console.log('p class:',p)console.log('m class:',m)Person.get();Ming.get();// p.get() // Uncaught TypeError: p.get is not a function// m.get() // Uncaught TypeError: m.get is not a function
私有属性
私有属性需要前面加 #。⚠️:除了私有属性还需要注意,前面没有加this。这不是单单是静态属性。其他属性定义时候也不需要添加this就可以。
class Person{// 私有属性#size = 1;getSize(){return this.#size;}}const p = new Person();console.log(p.size); // undefinedconsole.log(p.getSize());console.log(p)

