原型对象加冒充组合继承
// 原型对象加对象冒充组合继承
function Person(name, type) {
this.name = name;
this.type = type;
this.say = () => {
console.log(`${this.name}喜欢${this.type}`)
}
}
Person.prototype.sex = '男'
Person.prototype.hoy = () => {
console.log('hoy.....')
}
function Son(name, age) {
//继承构造函数的属性和方法 call :string apply : Array
// Person.call(this, age, name)
Person.apply(this, [age, name])
}
//继承构造韩式的原型链的属性和方法
Son.prototype = new Person()
//Son构造函数的constructor 需要重新指向 Son构造函数
Son.prototype.constructor = Son
Son.prototype.test = '测试'
Son.prototype.func = () => {
console.log('方法')
}
const newSon = new Son('张三', '篮球')
console.log(newSon)
newSon.say()
newSon.hoy()
newSon.func()
console.log(newSon.sex)
console.log(newSon.test)
console.log(newSon.__proto__)
es6类继承
class Father {
static a = 20;
static b = 30;
constructor() {
this.name = '三三'
this.firstName = '冯'
}
get() {
console.log(`我的名字叫${this.firstName}${this.name}`)
}
}
const newFather = new Father();
newFather.get()
console.log(newFather, '00')
console.log(newFather.name)
console.log(newFather.firstName)
console.log(Father.a)
console.log(Father.b)
class Son extends Father {
constructor() {
super()
}
}
const newSon = new Son();
console.log(newSon, '11')
console.log(Son.a)
console.log(Son.b)
newSon.get()
|