1、知识点
ES6类定义
2、实战
/**
* 1.关键字class
* 2.Player类名
* 3.constructor构造器,里面内容付给模板级的name,sex
* 4.定义个函数,继续引用模板
* 5.static ,静态方法->这个类不用实例化,就能引用
*/
class Player {
constructor(name, sex) {
this.name = name;
this.sex = sex;
}
show(){
console.log(`${this.name}的性别是${this.sex}。`);
}
static info(){
console.log("这是一个球员类,您可以使用它建立自己的球员。");
}
}
/**
* 1.定义一个player的变量
* 2.输出,默认的属性,name,sex
* 3.palyer.show()引用show()方法
*/
let player = new Player("admin", "男");
console.log(player.name, player.sex);
player.show();
/**
* 静态方法引用
*/
Player.info();
输出: