class类的定义

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

posted @ 2018-09-07 14:08  一叶*秋  阅读(1294)  评论(0编辑  收藏  举报

博客侧边栏公告广告位招租