const hhh = Symbol("hhh");
//创建一个类
class Person {
constructor(option) {
//实例属性
this.name = option.name;
this.age = option.age;
}
//原型方法
sayName() {
console.log("我是" + this.name);
this[hhh]();
}
//定义私有方法
[hhh]() {
console.log("我是私有方法");
}
//定义静态方法
static aaa() {
console.log("我是静态方法");
}
}
//向 Person 原型添加方法
Object.assign(Person.prototype, {
outAge() {
console.log("我今年" + this.age + "岁")
},
})
//实例化 Person 类 => xm
var xm = new Person({
name: "小明",
age: 18
})
//调用原型方法
xm.sayName();
xm.outAge();
//调用静态方法
Person.aaa();
//继承
class Man extends Person {
constructor(option) {
//这里的super等于父类的constructor
super(option);
this.job = option.job;
}
outJob(){
console.log("我是一名:"+this.job);
}
}
var jon = new Man({
name: "jon",
job: "doctor"
})
jon.sayName();
jon.outJob();
console