// 创建类
class Person {
constructor(name,age,hobby){
this.name = name
this.age = age
this.hobby = hobby
}
}
let LDH = new Person('刘德华',18,'唱歌')
console.log(LDH)
// 继承类
class Man extends Person {
constructor(name,age,hobby,say){
super(name, age, hobby)
this.say = say
}
}
let XM = new Man('小明',20,'跳舞','说话')
console.log(XM)
function Person(name,age,hobby){
// 判断this是否是Person的实例对象
// 如果不是,说明是通过Person()来调用的
// 此时,我们动手通过new来创建实例对象
if(!(this instanceof Person)){
return new Person(name,age,hobby)
}
this.name = name
this.age = age
this.hobby = hobby
}
const P1 = new Person()
const P2 = Person()
网友评论