欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[js高手之路]从原型链开始图解继承到组合继承的产生详解

程序员文章站 2022-09-08 22:42:46
于javascript原型链的层层递进查找规则,以及原型对象(prototype)的共享特性,实现继承是非常简单的事情 一、把父类的实例对象赋给子类的原型对象(proto...

于javascript原型链的层层递进查找规则,以及原型对象(prototype)的共享特性,实现继承是非常简单的事情

一、把父类的实例对象赋给子类的原型对象(prototype),可以实现继承

function person(){
 this.username = 'ghostwu';
 }
 person.prototype.showusername = function(){
 return this.username;
 }
 function teacher (){}
 teacher.prototype = new person();

 var ot = new teacher(); 
 console.log( ot.username ); //ghostwu
 console.log( ot.showusername() ); //ghostwu

通过把父类(person)的一个实例赋给子类teacher的原型对象,就可以实现继承,子类的实例就可以访问到父类的属性和方法

[js高手之路]从原型链开始图解继承到组合继承的产生详解

如果你不会画这个图,你需要去看下我的这篇文章:

第11行,执行ot.username, 首先去ot对象上查找,很明显ot对象上没有任何属性,所以就顺着ot的隐式原型__proto__的指向查找到teacher.prototype,

发现还是没有username这个属性,继续沿着teacher.prototype.__proto__向上查找,找到了new person() 这个实例上面有个username,值为ghostwu

所以停止查找,输出ghostwu.

第12行,执行ot.showusername前面的过程同上,但是在new person()这个实例上还是没有查找到showusername这个方法,继续沿着new person()的

隐式原型__proto__的指向( person.prototype )查找,在person.prototype上找到了showusername这个方法,停止查找,输出ghostwu.

二、把父类的原型对象(prototype)赋给子类的原型对象(prototype),可以继承到父类的方法,但是继承不到父类的属性

function person(){
 this.username = 'ghostwu';
 }
 person.prototype.showusername = function(){
 return 'person::showusername方法';
 }
 function teacher (){}
 teacher.prototype = person.prototype;

 var ot = new teacher(); 
 console.log( ot.showusername() ); //ghostwu
 console.log( ot.username ); //undefined, 没有继承到父类的username

因为teacher.prototype的隐式原型(__proto__)只指向person.prototype,所以获取不到person实例的属性

三、发生继承关系后,实例与构造函数(类)的关系判断

还是通过instanceof和isprototypeof判断

function person(){
 this.username = 'ghostwu';
 }
 person.prototype.showusername = function(){
 return this.username;
 }
 function teacher (){}
 teacher.prototype = new person();
 
 var ot = new teacher();
 console.log( ot instanceof teacher ); //true
 console.log( ot instanceof person ); //true
 console.log( ot instanceof object ); //true
 console.log( teacher.prototype.isprototypeof( ot ) ); //true
 console.log( person.prototype.isprototypeof( ot ) ); //true
 console.log( object.prototype.isprototypeof( ot ) ); //true

四,父类存在的方法和属性,子类可以覆盖(重写),子类没有的方法和属性,可以扩展

function person() {}
 person.prototype.showusername = function () {
 console.log('person::showusername');
 }
 function teacher() { }
 teacher.prototype = new person();
 teacher.prototype.showusername = function(){
 console.log('teacher::showusername');
 }
 teacher.prototype.showage = function(){
 console.log( 22 );
 }
 var ot = new teacher();
 ot.showusername(); //teacher::showusername
 ot.showage(); //22

五、重写原型对象之后,其实就是把原型对象的__proto__的指向发生了改变

原型对象prototype的__proto__的指向发生了改变,会把原本的继承关系覆盖(切断)

function person() {}
 person.prototype.showusername = function () {
 console.log('person::showusername');
 }
 function teacher() {}
 teacher.prototype = new person();
 teacher.prototype = {
 showage : function(){
 console.log( 22 );
 }
 }
 var ot = new teacher();
 ot.showage(); //22
 ot.showusername();

上例,第7行,teacher.prototype重写了teacher的原型对象(prototype),原来第6行的原型对象的隐式原型(__proto__)指向就没有作用了

所以在第14行,ot.showusername() 就会发生调用错误,因为teacher的原型对象(prototype)的隐式原型(__proto__)不再指向父类(person)的实例,继承关系被破坏了.

六、在继承过程中,小心处理实例的属性上引用类型的数据

function person(){
 this.skills = [ 'php', 'javascript' ];
 }
 function teacher (){}
 teacher.prototype = new person();

 var ot1 = new teacher();
 var ot2 = new teacher();
 ot1.skills.push( 'linux' );
 console.log( ot2.skills ); //php, java, linux

ot1的skills添加了一项linux数据,其他的实例都能访问到,因为其他实例*享了skills数据,skills是一个引用类型

七、借用构造函数

为了消除引用类型影响不同的实例,可以借用构造函数,把引用类型的数据复制到每个对象上,就不会相互影响了

function person( uname ){
 this.skills = [ 'php', 'javascript' ];
 this.username = uname;
 }
 person.prototype.showusername = function(){
 return this.username;
 }
 function teacher ( uname ){
 person.call( this, uname );
 }
 var ot1 = new teacher();
 ot1.skills.push( 'linux' );
 var ot2 = new teacher();
 console.log( ot2.skills ); //php,javascript
 console.log( ot2.showusername() );

虽然ot1.skills添加了一项linux,但是不会影响ot2.skills的数据,通过子类构造函数中call的方式,去借用父类的构造函数,把父类的属性复制过来,而且还能

传递参数,如第8行,但是第15行,方法调用错误,因为在构造中只复制了属性,不会复制到父类原型对象上的方法

八、组合继承(原型对象+借用构造函数)

经过以上的分析, 单一的原型继承的缺点有:

1、不能传递参数,如,

teacher.prototype = new person();
有些人说,小括号后面可以跟参数啊,没错,但是只要跟了参数,子类所有的实例属性,都是跟这个一样,说白了,还是传递不了参数

2、把引用类型放在原型对象上,会在不同实例上产生相互影响

单一的借用构造函数的缺点:

1、不能复制到父类的方法

刚好原型对象方式的缺点,借用构造函数可以弥补,借用构造函数的缺点,原型对象方式可以弥补,于是,就产生了一种组合继承方法:

function person( uname ){
 this.skills = [ 'php', 'javascript' ];
 this.username = uname;
 }
 person.prototype.showusername = function(){
 return this.username;
 }
 function teacher ( uname ){
 person.call( this, uname );
 }
 teacher.prototype = new person();

 var ot1 = new teacher( 'ghostwu' );
 ot1.skills.push( 'linux' );
 var ot2 = new teacher( 'ghostwu' );
 console.log( ot2.skills ); //php,javascript
 console.log( ot2.showusername() ); //ghostwu

子类实例ot2的skills不会受到ot1的影响,子类的实例也能调用到父类的方法。

以上这篇[js高手之路]从原型链开始图解继承到组合继承的产生详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。