class Person {

    // 以前的构造函数
    constructor(name, age, gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    };

    // 以前原型上的方法
    say() {
        console.log(`${ this.name }今年${ this.age }岁了`);
    };

};

// 使用方式一样
var xiaoming = new Person('小明', 14, '男');
xiaoming.say();

静态方法 静态方法属于类自己,通过类名.方法名调用 注意这里static关键字只能作用于方法,不能作用于属性

class Person {

constructor() {
Person.total++ || (Person.total = 1);
};

// 统计总人口,
static getTotal() {
return Person.total;
};

};

var p1 = new Person;
var p2 = new Person;
console.log(Person.getTotal()); // 2

相关文章:

  • 2022-12-23
  • 2021-06-07
  • 2021-10-05
  • 2022-12-23
  • 2021-12-27
  • 2021-09-18
猜你喜欢
  • 2022-02-27
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-05-06
  • 2022-12-23
相关资源
相似解决方案