一:属性表达式概念
类的属性名,可以采用表达式,使用【】来引用
const SHOW_INFO_FUNCTION_NAME = 'showDesc';
class Animal {
constructor(name,age) {
this.name = name;
this.age = age;
}
eat(){
return "Food";
}
showInfo(){
console.log("动物的信息:名称" + this.name + ",年龄" + this.age);
}
}
//继承
class Cat extends Animal {
constructor(name,age,color) {
super(name,age);
this.color = color;
}
eat() {
//调用父类的方法
let result = super.eat();
console.log("来自父类:",result);
return result + "fish"
}
// 重写showInfo方法
showInfo(){
console.log("毛的信息:名称" + this.name + ",年龄" + this.age + ",毛色" + this.color);
}
[SHOW_INFO_FUNCTION_NAME](){
console.log('自定义的方法,方法名称来自于属性表达式')
}
['a' + 'bc'](){
console.log('abc function')
}
}
let cat = new Cat('小黑',3,'black');
cat.showInfo();
let foods = cat.eat();
console.log(foods);
// 属性表达式方法的调用
cat[SHOW_INFO_FUNCTION_NAME]();
cat.abc();
cat['a' + 'bc']();

网友评论