下面看一个例子:
function C1(){}
function C2(){}
C1.prototype=C2.prototype;
var cc=new C1();
console.log(cc.__proto__===C1.prototype); //true
console.log(cc.__proto__===C2.prototype); //true
如果C1.prototype=C2.prototype;放在var cc=new C1();前面,那么cc的上一级原型对象是C1.prototype和C2.prototype;
再看第二个例子:
function C1(){}
function C2(){}
var cc=new C1();
C1.prototype=C2.prototype;
console.log(cc.__proto__===C1.prototype); //false
console.log(cc.__proto__===C2.prototype); //false
console.log(cc.__proto__===Object.prototype); //false
如果C1.prototype=C2.prototype;放在var cc=new C1();后面,那么cc的上一级原型对象不是C1.prototype和C2.prototype;更不是Object.prototype;
总结:如果要用空构造函数连接Parent那么最先要做的是用空函数H.prototype=Parent.prototype,然后再new空函数H,就能达到连接的作用。