function Food() {
this.type = 'food食物'
}
Food.prototype.getType = function () {
return this.type
}
function Vegetables(name) {
this.name = name
}
Vegetables.prototype = new Food()
const tomato = new Vegetables('tomato')
console.log(tomato.getType())
class Parent {
constructor(name) {
this.name = name
}
getName() {
return this.name
}
static getNames() {
return this.name
}
}
class child extends Parent {
constructor(name, age) {
super(name)
this.age = age
}
}
const newChild = new child('wanghui', 25)
console.log(newChild, newChild.name, newChild.getName())
console.log(newChild instanceof child, newChild instanceof Parent)
console.log(child.getNames())
console.log(Object.getPrototypeOf(child) === Parent)
class Father {
constructor() {
this.type = '爸爸'
}
getName() {
return this.type
}
}
Father.getType = () => {
return 'I am father'
}
class Son extends Father {
constructor() {
super()
this.type = '儿子'
}
getFatherName() {
console.log('2222', super.getName())
}
static getFatherType() {
console.log('3333', super.getType())
}
sonPrint() {
super.getName()
}
}
const S = new Son()