混合模式
知识点一:定义
知识点二:具体例子
//使用类继承实现mixin
class moduleA {
constructor(){
this.name = 'moduleA';
this.city = 'guangzhou';
}
getName(){
return this.name
}
say(){
return 'hi, i\'m '+ this.name
}
}
//moduleB做的就是在moduleA基础上做扩充,而且可以在say方法上做修改
class moduleB extends moduleA{
constructor(){
super();
this.age = 12;
this.city = 'shenzhen';
}
getAge(){
return this.age
}
say(){
const origin = super.say();
return 'hi, i\'m the new methods, the origin is say:'+ origin
}
}
const module = new moduleB()
console.log(module.say());
//使用对象继承实现mixin
const fooA = {
a:1,
b:2
}
const fooB = Object.create(fooA,{c:3});
console.log(fooB);知识点三:特点
Last updated
Was this helpful?