// js中,为了解决继承问题。
//创建了Person类,然后需要创建Man和Woman两个继承类,继承类需要继承Person内的属性。解决这一需求。
/* 1. 原型链继承*/
/*
function Parent1() {
this.name = 'parent1';
this.play = [1, 2, 3]
}
function Child1() {
this.type = 'child2';
}
console.dir(new Child1()); // 此时prototype是Function
Child1.prototype = new Parent1();
console.dir(new Child1()); // 此时是 Parent1
// 潜在问题.修改了一个属性,会导致其他的属性同时变更。侧面证明:指针指向同一块内存,所以会同时被改变。
let s1 = new Child1();
let s2 = new Child1();
console.log(s1.play,s2.play) // 1,2,3,, 1,2,3,
s1.play.push(4)
console.log(s1.play,s2.play) // 1,2,3,4, 1,2,3,4
*/
/** 2. 构造函数继承,解决prototype共享问题 */
/*
function Parent1() {
this.name = 'parent1';
}
console.log(Parent1.prototype);
Parent1.prototype.getName = function () {
return this.name;
}
function Child1() {
Parent1.call(this);
this.type = 'child1';
}
let child1 = new Child1();
let child2 = new Child1();
child1.name = 888;
console.log(child1.name,child2.name);
console.log(child1) */
/** 3.组合继承,前两种组合 */
function Parent3 () {
this.name = 'parent3';
this.play = [1,2,3];
}
Parent3.prototype.getName = function () {
return this.name
}
function Child3 () {
Parent3.call(this)
this.type = 'child3';
}
console.log(new Child3());
Child3.prototype = new Parent3();
// 此时,Child3.prototype.constructor = Parent3
console.log(new Child3());
Child3.prototype.constructor = Child3;
let s3 = new Child3();
let s4 = new Child3();
s3.play.push(4);
console.log(s3.play,s4.play);
console.log(s3.getName())
console.log(s4.getName())
console