鸟食轩
Microsoft .NET[C#] MVP 2003
随笔 - 332, 文章 - 870, 评论 - 5722, 引用 - 356
在JavaScript面向对象编程中使用继承(1)
前几天做了一个
JScript版的CollecionBase类
,用来解决需要使用集合作为主要数据结构的类的基类。不过当时挺忙的没有给出继承的示例,搞得有的网友对JavaScript继承比较迷惑,于是今天使用四种方式来分别实现了4个ArrayList派生类。
关于使用JavaScript进行面向对象编程(OOP),网上已有很多的文章说过了。这里我推荐两篇文章大家看看,如果没有理解怎么使用JavaScript的Function对象的prototype属性来实现类定义及其原理,那么就仔细看看'
面向对象的JavaScript编程
'、'
面向对象的Jscript
'和'
Classical Inheritance in JavaScript
'哦(特别是第一篇及其相关讨论的文章),否则后面一头雾水不能怪我啦
。
那个CollectionBase就不列了,第一个链接就是它了,下面是用四种方法实现的JavaScript'继承'(以后就不打引号了,反正知道JavaScript的继承不是经典OO里的继承就行了):
构造继承法:
<
script
language
="javascript"
>
function
ArrayList01()
{
this
.base
=
CollectionBase;
this
.base();
this
.m_Array
=
this
.m_InnerArray;
this
.foo
=
function
()
{
document.write(
this
+
': '
+
this
.m_Count
+
': '
+
this
.m_Array
+
'
<
br
/>
');
}
;
this
.Add
=
function
(item)
{
this
.InsertAt(item,
this
.m_Count);
}
;
this
.InsertAt
=
function
(item, index)
{
this
.m_InnerArray.splice(index,
0
, item);
this
.m_Count
++
;
}
;
this
.toString
=
function
()
{
return
'[class ArrayList01]';
}
;
}
</
script
>
原形继承法:
<
script
language
="JavaScript"
>
function
ArrayList02()
{
this
.InsertAt
=
function
(item, index)
{
this
.m_InnerArray.splice(index,
0
, item);
this
.m_Count
++
;
}
;
this
.m_Array
=
this
.m_InnerArray;
this
.toString
=
function
()
{
return
'[class ArrayList02]';
}
;
}
ArrayList02.prototype
=
new
CollectionBase();
ArrayList02.prototype.foo
=
function
()
{
document.write(
this
+
': '
+
this
.m_Count
+
': '
+
this
.m_Array
+
'
<
br
/>
');
}
;
ArrayList02.prototype.InsertAt
=
function
(item, index)
{
this
.m_InnerArray.splice(index,
0
, item);
this
.m_Count
++
;
}
;
</
script
>
实例继承法:
<
script
language
="javascript"
>
function
ArrayList03()
{
var
base
=
new
CollectionBase();
base.m_Array
=
base.m_InnerArray;
base.foo
=
function
()
{
document.write(
this
+
': '
+
this
.m_Count
+
': '
+
this
.m_Array
+
'
<
br
/>
');
}
;
base.InsertAt
=
function
(item, index)
{
this
.m_InnerArray.splice(index,
0
, item);
this
.m_Count
++
;
}
;
base.toString
=
function
()
{
return
'[class ArrayList03]';
}
;
return
base;
}
</
script
>
附加继承法:
<
script
language
="JavaScript"
>
function
ArrayList04()
{
this
.base
=
new
CollectionBase();
for
(
var
key
in
this
.base )
{
if
(
!
this
[key] )
{
this
[key]
=
this
.base[key];
}
}
this
.m_Array
=
this
.m_InnerArray;
this
.InsertAt
=
function
(item, index)
{
this
.m_InnerArray.splice(index,
0
, item);
this
.m_Count
++
;
}
;
}
ArrayList04.prototype.foo
=
function
()
{
document.write(
this
+
': '
+
this
.m_Count
+
': '
+
this
.m_Array
+
'
<
br
/>
');
}
ArrayList04.prototype.toString
=
function
()
{
return
'[class ArrayList04]';
}
</
script
>
派生类中的foo是一个新增加的函数,用来输出类的类型和m_InnerArray里的数据。toString()相当于override了CollectionBase中的toString(),不过其实就是赋值覆盖,和'
从JavaScript函数重名看其初始化方式
'一文中说到的原理是一样的。这个四种方法的原理和区别我
稍候再作分析
,要
了...
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2005-01-28 22:35
birdshome
阅读(12741)
评论(13)
编辑
收藏
评论
1483388
#1楼
回复
引用
大师真是诲人不倦哪!犹如大海上的一盏明灯啊!
2005-01-29 21:58
|
Boler Guo
#2楼
[
楼主
]
回复
引用
查看
这帽子扣的还能做事儿吗?! @_@
2005-01-31 18:00
|
birdshome
#3楼
回复
引用
查看
大海上的一盏明灯。。。。。。海上要看灯塔,伸手不见五指的夜里才看明灯,哈哈哈哈
2005-02-01 09:30
|
阮
#4楼
回复
引用
查看
2005年。。。。。
2008-03-27 22:28
|
胥子维
#5楼
回复
引用
查看
Mark
2008-08-02 10:35
|
簡簡單單..
#6楼
回复
引用
2009年1月4日学习
2009-01-04 13:39
|
无痕羽[未注册用户]
#7楼
回复
引用
查看
膜拜。2005年写的。小弟钦佩!
2009-03-21 18:04
|
Alone
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
春节后礼品回收iPhone成新宠燕窝被冷落
·
分析称苹果近1000亿现金储备最佳用途是派息
·
扎克伯格11件蠢事:曾同意将Facebook卖给雅虎
·
最想要的Entity Framework功能
·
专访Jeffrey Richter:Windows 8是微软的重中之重
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
导航
博客园
首页
新随笔
联系
订阅
管理
公告
Invert
原创
技术文章和心得,
转载
必须注明来源
"博客园"
!
贴子以"现状"提供,且没有任何担保,同时也没有授予任何权利。
昵称:
birdshome
园龄:
7年10个月
荣誉:
推荐博客
粉丝:
73
关注:
3
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
我的标签
Nokia 6300
(2)
fan
(1)
qq
(1)
腾讯
(1)
输入法
(1)
隐私
(1)
随笔分类
(337)
.NET的私有工具类(2)
(rss)
.NET技术开发相关(34)
(rss)
Asp.net控件开发(14)
(rss)
Debug中的滑铁卢(9)
(rss)
Enjoy Computer :)(27)
(rss)
Jscript&Dhtml开发(162)
(rss)
技术区里的非技术(49)
(rss)
开发中遇到的虫子(14)
(rss)
其它编程相关内容(16)
(rss)
商务智能(BI)开发(10)
(rss)
文章分类
(147)
北京的幸福生活(29)
(rss)
不得不转载(19)
(rss)
乖乖的文章(6)
(rss)
那时还没有blog(4)
(rss)
那是相当八卦(10)
(rss)
手机短信息(1)
(rss)
我们的文章(40)
(rss)
珍贵照片的背景(4)
(rss)
重庆的幸福生活(34)
(rss)
相册
2005 Kick Off @ 涞滩
2006 Kick Off @ 钓鱼城
2006 Offsite @ 四面山
2006 Wedding @ 昆明
2007 Marriage @ 哈尔滨
2007 Offsite @ Thailand
2007 Offsite @ 海南云天
2007 Training @ Seattle
2007 Travelling @ 云南
2008 Travelling @ 天津
乱七八糟的收集
Ex-Colleagues
Jason Lei's Space
.Net Life
(rss)
Jasper
(rss)
Michelles Space
xingd.net
(rss)
玻璃缸
(rss)
短鲷生活
海阔天空
录一室
(rss)
完美的泡菜
(rss)
怡红公子
(rss)
竹叶尖的BLOG
常用链接
Engadget 中文版
PDFCHM eBooks
SitePoint Forums
The Code Project
安利上海直销店
哈尔滨工业大学
哈工大紫丁香
海归论坛
(rss)
沙坪坝区中医院
兄弟情深
没有可乐的日子
葡萄树下不乘凉
(rss)
斯普特尼克
猪头小队长
(rss)
积分与排名
积分 - 3145044
排名 - 6
最新评论
阅读排行榜
推荐排行榜