class是一个对象{},constructor是一个函数(){}
// 我要被下面所继承
class Game {
constructor(name, type) {
this.name = name
this.type = type
}
}
// 我要继承上面的属性
class Role extends Game { //语法: 要继承 extends 被继承
constructor(roleName, hit, sex, game_name, game_type) { //这里要将被继承的属性写入形参,下面用super接受
super(game_name, game_type) //super需要接受上面的形参
this.roleName = roleName
this.hit = hit
this.sex = sex
}
dance() {
console.log('跳舞');
}
}
实例化对象
let game = new Role('程咬金', '800点攻击力', '男', '王者荣耀', '手游')
console.log(game);
game.dance()
完整写法如下
// 我要被下面所继承
class Game {
constructor(name, type) {
this.name = name
this.type = type
}
}
// 我要继承上面的属性
class Role extends Game { //语法: 要继承 extends 被继承
constructor(roleName, hit, sex, game_name, game_type) { //这里要将被继承的属性写入形参,下面用super接受
super(game_name, game_type) //super需要接受上面的形参
this.roleName = roleName
this.hit = hit
this.sex = sex
}
dance() {
console.log('跳舞');
}
}
let game = new Role('程咬金', '800点攻击力', '男', '王者荣耀', '手游')
console.log(game);
game.dance()
效果图