构造函数继承
function Parent(name){
this.name = name
}
Parent.protoType.testProto = 1;
function Child(name){
Parent.call(this, name)
}
const child1 = new Child('test');
console.log(child1.testProto)
const child2 = new Child('test1');
原型链继承
function Parent(){
this.name = 'test'
}
Parent.protoType.test = 'test1'
function Child(){}
Child.protoType = new Parent();
const child1 = new Child()
console.log(child1.test)
console.log(child1.name)
组合式继承
function Parent(name) {
this.name = name;
}
Parent.protoType.test = '1';
function Child(name) {
Parent.call(this,name);
this.child = 'child'
}
Child.protoType = new Parent();
Child.protoType.constructor = Child;
const child1 = new Child('test');
原型式继承
const obj = {
test: 'test'
}
const newObj = Object.create(obj, {
test1: {
value:"test1",
writable:true,
configurable:true,
enumerable:true
},
})
console.log(newObj)
function createObj(o) {
function F() {};
F.protoType = o;
return new F();
}
寄生式继承
function createObj(o) {
const clone = Object.create(o);
return clone;
}
组合寄生式继承
function Parent(name) {
this.name = name;
}
function Child(name) {
Parent.call(this,name);
}
function F(){}
F.protoType = Parent.protoType;
Child.protoType = new F()
const child1 = new Child('test')