在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/,如需转载请自行联系原博主。

目录
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript 继承的方式和优缺点
JavaScript 继承的方式和优缺点
13 0
|
JavaScript 前端开发 Java
深入JS面向对象(原型-继承)(三)
深入JS面向对象(原型-继承)
31 0
|
JavaScript 前端开发 Java
深入JS面向对象(原型-继承)(一)
深入JS面向对象(原型-继承)
32 0
|
2月前
|
JavaScript 前端开发
js开发:请解释原型继承和类继承的区别。
JavaScript中的原型继承和类继承用于共享对象属性和方法。原型继承利用原型链查找属性,节省内存但不支持私有成员。类继承通过ES6的class和extends实现,支持私有成员但占用更多内存。两者各有优势,适用于不同场景。
20 0
|
4月前
|
JavaScript
|
7天前
|
JavaScript 前端开发
【JavaScript技术专栏】JavaScript中的面向对象编程
【4月更文挑战第30天】本文介绍了JavaScript中的面向对象编程,包括对象的基本概念、创建对象的方法(字面量、构造函数、Object.create()和ES6的class)及继承机制(原型链和ES6的class继承)。面向对象编程通过抽象为对象,实现了代码复用和模块化,提高了程序的可读性和可维护性。
|
8天前
|
JavaScript 前端开发
JavaScript 继承的方式和优缺点
JavaScript 继承的方式和优缺点
|
11天前
|
JavaScript 前端开发
快速理解什么是JavaScript的继承
在这个示例中,`Dog`子类继承了 `Animal`父类的属性和方法,使得 `myDog`实例能够访问 `sayHello`方法。这就是JavaScript中继承的基本概念。
26 1
|
25天前
|
JavaScript 前端开发
深入探讨JavaScript中的原型链与继承机制
JavaScript作为一种灵活而强大的编程语言,其独特的原型链与继承机制是其核心特性之一。本文将深入探讨JavaScript中的原型链与继承机制,从基础概念到实际应用,帮助读者更好地理解和利用JavaScript的继承特性。
|
1月前
|
JavaScript
未能正确利用原型继承(js的问题)
未能正确利用原型继承(js的问题)