在JavaScript面向对象编程中使用继承(1)

简介:
前几天做了一个 JScript版的CollecionBase类 ,用来解决需要使用集合作为主要数据结构的类的基类。不过当时挺忙的没有给出继承的示例,搞得有的网友对JavaScript继承比较迷惑,于是今天使用四种方式来分别实现了4个ArrayList派生类。

    关于使用JavaScript进行面向对象编程(OOP),网上已有很多的文章说过了。这里我推荐两篇文章大家看看,如果没有理解怎么使用JavaScript的Function对象的prototype属性来实现类定义及其原理,那么就仔细看看'
面向对象的JavaScript编程 '、' 面向对象的Jscript '和' Classical Inheritance in JavaScript '哦(特别是第一篇及其相关讨论的文章),否则后面一头雾水不能怪我啦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(),不过其实就是赋值覆盖,和'从JavaScript函数重名看其初始化方式'一文中说到的原理是一样的。这个四种方法的原理和区别我稍候再作分析,要了...


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

目录
相关文章
|
JavaScript 前端开发 Java
深入JS面向对象(原型-继承)(一)
深入JS面向对象(原型-继承)
30 0
|
27天前
|
JavaScript 前端开发
js开发:请解释原型继承和类继承的区别。
JavaScript中的原型继承和类继承用于共享对象属性和方法。原型继承利用原型链查找属性,节省内存但不支持私有成员。类继承通过ES6的class和extends实现,支持私有成员但占用更多内存。两者各有优势,适用于不同场景。
18 0
|
3月前
|
JavaScript
|
3月前
|
存储 前端开发 JavaScript
揭秘原型链:探索 JavaScript 面向对象编程的核心(下)
揭秘原型链:探索 JavaScript 面向对象编程的核心(下)
揭秘原型链:探索 JavaScript 面向对象编程的核心(下)
|
3月前
|
前端开发 JavaScript 开发者
揭秘原型链:探索 JavaScript 面向对象编程的核心(上)
揭秘原型链:探索 JavaScript 面向对象编程的核心(上)
揭秘原型链:探索 JavaScript 面向对象编程的核心(上)
|
3月前
|
存储 JavaScript 前端开发
构造函数和原型的结合应用:轻松搞定JS的面向对象编程(三)
构造函数和原型的结合应用:轻松搞定JS的面向对象编程
|
3月前
|
设计模式 JavaScript 前端开发
构造函数和原型的结合应用:轻松搞定JS的面向对象编程(一)
构造函数和原型的结合应用:轻松搞定JS的面向对象编程
|
3月前
|
存储 JavaScript 前端开发
构造函数和原型的结合应用:轻松搞定JS的面向对象编程(二)
构造函数和原型的结合应用:轻松搞定JS的面向对象编程
|
1月前
|
设计模式 JavaScript 前端开发
JavaScript中继承的优缺点
JavaScript中继承的优缺点
13 3
|
1月前
|
JavaScript 前端开发
如何在 JavaScript 中实现继承?
如何在 JavaScript 中实现继承?
11 2