一、继承原理
原型链
不知道什么是原型链?
来读几个关键词:
哥欧 构 构造函数 构造函数 构造函数
实例 实例 实例
原型对象 原型对象 原型对象
prototype prototype prototype
__proto__ __proto__ __proto__
constructor constructor constructor
instanceof instanceof instanceof
实现instanceof
function newInstanceof (left, right) {
let leftProto = left.__proto__;
let rightPrototype = right.prototype
while(true) {
if(leftProto === rightPrototype) {
return true;
} else if (leftProto === null) {
return false;
} else {
leftProto = leftProto.__proto__
}
}
}
二、继承方法
1、构造函数
function FatherA() {
this.money = 100000;
}
function ChildA() {
FatherA.call(this);
this.soliloquy = '开心';
}
var childAng = new ChildA();
FatherA.prototype.makeMoneyWay = function () {
console.log('next lottery number is ……');
}
假设某地有个习俗,FatherA会给ChildA准备10万教育基金,
构造函数继承可以完美的让孩子获取到准备好的教育基金。
然而这个时候FatherA新领悟到了一串神秘代码,childAng却用不了
childAng.makeMoneyWay(); // 会报错
这样的继承感觉不是亲身的。
2、原型链继承
function FatherB() {
this.money = 100000;
this.card = ['工商', '建设'];
}
function ChildB() {
this.soliloquy = '开心';
}
ChildB.prototype = new FatherB()
var childBobo = new ChildB();
var childBigShuan = new ChildB();
FatherB.prototype.makeMoneyWay = function () {
console.log('next lottery number is ……');
}
childBobo.card.push('招商'); // 引用类型受影响
childBobo.money += 1000; // 值类型在此不受影响
原型链继承解决了构造函数的问题,FatherB上新的技能,ChildB的实例也能很好的使用。
图示:
然而一个新问题就是,当实例有多个例如childBobo(波波)和childBigSuan(大栓)。
波波这个时候新办了一张银行卡,结果大栓立马能用,这样让波波觉得很没有隐私,哦不,是很不公平,凭啥你刷我的卡。
3、组合方式继承
function FatherC() {
this.money = 100000;
this.card = ['工商', '建设'];
}
function ChildC() {
FatherC.call(this);
this.soliloquy = '开心';
}
ChildC.prototype = new FatherC()
var childCuifa = new ChildC();
var childChizi = new ChildC();
FatherC.prototype.makeMoneyWay = function () {
console.log('next lottery number is ……');
}
childCuifa.card.push('招商');
childCuifa.money += 1000;
出了问题总要解决,这时候出现了由构造函数和原型链的组合继承,这样既让ChildC实例的数据相互独立,也让ChildC能获取到FatherC的新技能。
社会问题是解决了。新的问题就是性能问题,这类继承会调用FatherC两次。显然用的越多,消耗的资源越多。(虽然现在硬件很发达,但是1个人睡100平米的床终究还是不方面,起夜还没下床呢就尿了)
4、组合方式继承进阶版
function FatherD() {
this.money = 100000;
this.card = ['工商', '建设'];
}
function ChildD() {
FatherD.call(this);
this.soliloquy = '开心';
}
ChildD.prototype = FatherD.prototype
var childDazhu = new ChildD();
var childDabing = new ChildD();
console.log('分不清是孩子还是父辈', childDazhu instanceof ChildD, childDazhu instanceof FatherD);
console.log('大柱的构造函数指向的是ParentD', childDazhu.constructor);
上一个组合继承中
ChildC.prototype = new FatherC()
这行代码主要是为了让ChildC.prototype与new FatherC()的__proto__在一条原型链上。
所以换个思路,new FatherC()的__proto__也就是FatherC.protorype。
图示:
替换后,既不会再执行FatherC,也让原型链合并到了一起。
这个方案的隐藏问题就是,childDazhu(大柱)这个实例,会让人不知道是孩子还是爸爸。
5、组合方式继承终极版
function FatherE() {
this.money = 100000;
this.card = ['工商', '建设'];
}
function ChildE() {
FatherE.call(this);
this.soliloquy = '开心';
}
ChildE.prototype = Object.create(FatherE.prototype);
ChildE.prototype.constructor = ChildE;
var childErgo = new ChildE();
var childErwa = new ChildE();
var fatherErjun = new FatherE();
console.log('能分清是孩子还是父辈', childErgo instanceof ChildE, childErgo instanceof FatherE);
console.log('二狗的构造函数已指向childE', childErgo.constructor);
图示:
关键代码
Object.create
Object.create创建对象的方法就是用原型链来连接的。
Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。
参考自:
https://developer.mozilla.org...
var a = {name:'a'};
var b = Object.create(a);
b.__proto__ === a; // true
这样ChildE与FatherE.prototype就在一条链上面了,并且数据也进行了隔离,此时修改ChildE原型对象的构造函数指向,不会影响到FatherE的原型对象。
也让childErgo(二狗)有了确定的归属。
幸福的结局!!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。