博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在JavaScript面向对象编程中使用继承(1)
阅读量:7003 次
发布时间:2019-06-27

本文共 2632 字,大约阅读时间需要 8 分钟。

前几天做了一个 ,用来解决需要使用集合作为主要数据结构的类的基类。不过当时挺忙的没有给出继承的示例,搞得有的网友对JavaScript继承比较迷惑,于是今天使用四种方式来分别实现了4个ArrayList派生类。
    关于使用JavaScript进行面向对象编程(OOP),网上已有很多的文章说过了。这里我推荐两篇文章大家看看,如果没有理解怎么使用JavaScript的Function对象的prototype属性来实现类定义及其原理,那么就仔细看看' '、' '和' '哦(特别是第一篇及其相关讨论的文章),否则后面一头雾水不能怪我啦
emwink.gif
    那个CollectionBase就不列了,第一个链接就是它了,下面是用四种方法实现的JavaScript'继承'(以后就不打引号了,反正知道JavaScript的继承不是经典OO里的继承就行了):
    构造继承法:
ExpandedBlockStart.gif
<
script 
language
="javascript"
>
InBlock.gif
function  ArrayList01()
ExpandedSubBlockStart.gif {
InBlock.gif    
this.base =  CollectionBase;
InBlock.gif    
this .base(); 
InBlock.gif    
InBlock.gif    
this.m_Array = 
this .m_InnerArray;
InBlock.gif     
InBlock.gif    
this.foo = 
function ()
ExpandedSubBlockStart.gif     {
InBlock.gif        document.write(
this + ': ' + 
this.m_Count + ': ' + 
this.m_Array + '<br /> ');
ExpandedSubBlockEnd.gif    } ;
InBlock.gif   
InBlock.gif    
this.Add = 
function (item)
ExpandedSubBlockStart.gif     {
InBlock.gif        
this.InsertAt(item, 
this .m_Count);
ExpandedSubBlockEnd.gif    } ;
InBlock.gif    
InBlock.gif    
this.InsertAt = 
function (item, index)
ExpandedSubBlockStart.gif     {
InBlock.gif        
this.m_InnerArray.splice(index, 0 , item);
InBlock.gif        
this.m_Count++ ;
ExpandedSubBlockEnd.gif    } ;
InBlock.gif   
InBlock.gif    
this.toString = 
function ()
ExpandedSubBlockStart.gif     {
InBlock.gif        
return  '[class ArrayList01]';
ExpandedSubBlockEnd.gif    } ;
ExpandedBlockEnd.gif}
None.gif
</
script
>
    原形继承法:
ExpandedBlockStart.gif
<
script 
language
="JavaScript"
>
InBlock.gif
function  ArrayList02()
ExpandedSubBlockStart.gif {   
InBlock.gif    
this.InsertAt = 
function (item, index)
ExpandedSubBlockStart.gif     {
InBlock.gif        
this.m_InnerArray.splice(index, 0 , item);
InBlock.gif        
this.m_Count++ ;
ExpandedSubBlockEnd.gif    } ;
InBlock.gif   
InBlock.gif    
this.m_Array = 
this .m_InnerArray;
InBlock.gif 
InBlock.gif    
this.toString = 
function ()
ExpandedSubBlockStart.gif     {
InBlock.gif        
return  '[class ArrayList02]';
ExpandedSubBlockEnd.gif    } ;
ExpandedSubBlockEnd.gif}
InBlock.gif 
InBlock.gifArrayList02.prototype  = 
new  CollectionBase();
InBlock.gif
InBlock.gifArrayList02.prototype.foo = 
function ()
ExpandedSubBlockStart.gif {
InBlock.gif     document.write(
this + ': ' + 
this.m_Count + ': ' + 
this.m_Array + '<br /> ');
ExpandedSubBlockEnd.gif} ;
InBlock.gif
InBlock.gifArrayList02.prototype.InsertAt = 
function (item, index)
ExpandedSubBlockStart.gif {
InBlock.gif    
this.m_InnerArray.splice(index, 0 , item);
InBlock.gif    
this.m_Count++ ;
ExpandedBlockEnd.gif} ;
None.gif
</
script
>
    实例继承法:
ExpandedBlockStart.gif
<
script 
language
="javascript"
>
InBlock.gif
function  ArrayList03()
ExpandedSubBlockStart.gif {
InBlock.gif    
var base = 
new  CollectionBase();
InBlock.gif   
InBlock.gif    base.m_Array =  base.m_InnerArray;
InBlock.gif     
InBlock.gif    base.foo = 
function ()
ExpandedSubBlockStart.gif     {
InBlock.gif        document.write(
this + ': '+ 
this.m_Count + ': ' + 
this.m_Array + '<br /> ');
ExpandedSubBlockEnd.gif    } ;
InBlock.gif   
InBlock.gif    base.InsertAt = 
function (item, index)
ExpandedSubBlockStart.gif     {
InBlock.gif        
this.m_InnerArray.splice(index, 0 , item);
InBlock.gif        
this.m_Count++ ;
ExpandedSubBlockEnd.gif    } ;
InBlock.gif   
InBlock.gif    base.toString = 
function ()
ExpandedSubBlockStart.gif     {
InBlock.gif        
return  '[class ArrayList03]';
ExpandedSubBlockEnd.gif    } ;
InBlock.gif    
return  base;
ExpandedBlockEnd.gif}
None.gif
</
script
>
    附加继承法:
ExpandedBlockStart.gif
<
script 
language
="JavaScript"
>
InBlock.gif
function  ArrayList04()
ExpandedSubBlockStart.gif {
InBlock.gif    
this.base = 
new  CollectionBase();
InBlock.gif    
InBlock.gif    
for ( 
var key 
in 
this .base )
ExpandedSubBlockStart.gif     {
InBlock.gif        
if ( !
this [key] )
ExpandedSubBlockStart.gif         {
InBlock.gif            
this[key] = 
this .base[key];
ExpandedSubBlockEnd.gif        }  
ExpandedSubBlockEnd.gif    }
InBlock.gif    
InBlock.gif    
this.m_Array = 
this .m_InnerArray;
InBlock.gif     
InBlock.gif    
this.InsertAt = 
function (item, index)
ExpandedSubBlockStart.gif     {
InBlock.gif        
this.m_InnerArray.splice(index, 0 , item);
InBlock.gif        
this.m_Count++ ;
ExpandedSubBlockEnd.gif    } ; 
ExpandedSubBlockEnd.gif}
InBlock.gif
InBlock.gifArrayList04.prototype.foo  = 
function ()
ExpandedSubBlockStart.gif {
InBlock.gif    document.write(
this + ': ' + 
this.m_Count + ': ' + 
this.m_Array + '<br /> ');
ExpandedSubBlockEnd.gif}
InBlock.gif
InBlock.gifArrayList04.prototype.toString  = 
function ()
ExpandedSubBlockStart.gif {
InBlock.gif    
return  '[class ArrayList04]';
ExpandedBlockEnd.gif}
None.gif
</
script
>

    派生类中的foo是一个新增加的函数,用来输出类的类型和m_InnerArray里的数据。toString()相当于override了CollectionBase中的toString(),不过其实就是赋值覆盖,和''一文中说到的原理是一样的。这个四种方法的原理和区别我,要了...

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

你可能感兴趣的文章
Facebook存储技术方案:找出“暖性BLOB”数据
查看>>
Windows环境下载与安装JBOSS服务器的详细图文教程
查看>>
深入了解STL中set与hash_set,hash表基础
查看>>
python使用VBA:Excel创建图表(转)
查看>>
Git的冲突解决过程
查看>>
debug 输出 以及宏定义--备
查看>>
2016年某前端群题目答案参考
查看>>
spark scala学习笔记
查看>>
NeHe OpenGL教程 第三课:颜色渲染
查看>>
线程高级应用-心得1-传统线程和定时器讲解及案例分析
查看>>
struct timeval结构体 以及 gettimeofday()函数(转)
查看>>
HTTPS科普扫盲帖
查看>>
MFC 中的设计模式分析
查看>>
java--Struts中请求的过程
查看>>
VM虚拟机如何和主机共享文件夹或文件
查看>>
Chocolatey 简介(软件自动化管理工具)
查看>>
qml demo分析(photosurface-图片涅拉)
查看>>
BZOJ 2463: [中山市选2009]谁能赢呢?[智慧]
查看>>
Solidworks如何创建投影曲线
查看>>
鼠标悬浮tip 显示
查看>>