Snowdream
posts - 403, comments - 310, trackbacks - 0, articles - 7
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
JavaScript 学习 - Inheritance
Posted on 2007-05-05 18:57
ZelluX
阅读(278)
评论(0)
编辑
收藏
所属分类:
Web
1. Javascript中的继承要自己模拟实现。一些常用方法:
a) Object masquerading
function
ClassA(sColor)
{
this
.color
=
sColor;
this
.sayColor
=
function
()
{
alert(
this
.color);
}
;
}
function
ClassB(sColor, sName)
{
this
.newMethod
=
ClassA;
this
.newMethod(sColor);
delete
this
.newMethod;
this
.name
=
sName;
this
.sayName
=
function
()
{
alert(
this
.name);
}
;
}
通过调用ClassA的生成方法,ClassB继承了ClassA的属性和方法,同时newMethod所占用的空间也被释放。
另外,Object masquerading还支持多重继承,方法类似。
b) 使用call()方法
感觉这个方法类似于Java反射机制中的invoke方法,第一个参数是个调用的对象主体,后面是被调用方法的参数。
function
ClassB(sColor, sName)
{
ClassA.call(
this
, sColor);
this
.name
=
sName;
this
.sayName
=
function
()
{
alert(
this
.name);
}
;
}
ClassB通过调用ClassA的生成方法完成了初始化。
c) 使用apply()方法
和call()方法很相似,不同的是apply方法只有两个参数,一个是调用对象主体,一个是参数数组。
因此只要被上例的call语句改成ClassA.apply(this, new Array(sColor));即可
d) prototype链
function
ClassA()
{
}
ClassA.prototype.color
=
“red”;
ClassA.prototype.sayColor
=
function
()
{
alert(
this
.color);
}
;
function
ClassB()
{
}
ClassB.prototype
=
new
ClassA();
注意被继承的类ClassA构造方法里没有任何参数。
ClassB新的属性要在prototype被赋值后再添加,否则就会被删除。
这种方法的好处在于,使用instanceof判断子类对象和父类的关系的结果是true,和面向对象的思想一致。
e) 混合
Object masquerading的缺点在于性能不好,而prototype链又只能用无参构造器。因此要把两者结合起来。
function
ClassA(sColor)
{
this
.color
=
sColor;
}
ClassA.prototype.sayColor
=
function
()
{
alert(
this
.color);
}
;
function
ClassB(sColor, sName)
{
ClassA.call(
this
, sColor);
this
.name
=
sName;
}
ClassB.prototype
=
new
ClassA();
ClassB.prototype.sayName
=
function
()
{
alert(
this
.name);
}
;
要求ClassA在创建的时候也使用了prototype和constructor方法。
f) 动态创建的类的继承
function
Triangle(iBase, iHeight)
{
Polygon.call(
this
,
3
);
this
.base
=
iBase;
this
.height
=
iHeight;
if
(
typeof
Triangle._initialized
==
“undefined”)
{
Triangle.prototype.getArea
=
function
()
{
return
0.5
*
this
.base
*
this
.height;
}
;
Triangle._initialized
=
true
;
}
}
Triangle.prototype
=
new
Polygon();
注意prototype继承这一语句是在最后执行的,也不能被封装进构造器中。
2. zInherit库
简略的看了下,基本功能Prototype框架都提供。
3.xbObjects
同样是个库,略
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理
相关文章:
PHP + IIS 配置成功
zz网页的背景色参考
CSS Hacks
JavaScript 学习 - Inheritance
Firebug Lite for IE, Safari, Opera...
JavaScript 学习 - Object Basics
JavaScript 学习 - Functions
jQuery框架和Interface插件
小结下 Web应用课 Project 2
CSS - Setting the Foundations
Powered by:
BlogJava
Copyright © ZelluX
日历
<
2007年5月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(21)
给我留言
查看公开留言
查看私人留言
随笔分类
(390)
Algorithm(57)
C/C++(39)
Courses(15)
Economics(2)
Laboratory(25)
Linux(47)
Mathematics(12)
OOP(89)
Scripting(19)
Security(3)
System(28)
Web(10)
书、电影、音乐(11)
其他(14)
点滴(19)
随笔档案
(389)
2009年12月 (1)
2009年4月 (1)
2009年3月 (4)
2009年2月 (2)
2009年1月 (2)
2008年11月 (1)
2008年10月 (9)
2008年9月 (1)
2008年7月 (2)
2008年6月 (4)
2008年5月 (12)
2008年4月 (18)
2008年3月 (7)
2008年2月 (33)
2008年1月 (19)
2007年12月 (8)
2007年11月 (14)
2007年10月 (24)
2007年9月 (18)
2007年8月 (28)
2007年7月 (33)
2007年6月 (26)
2007年5月 (30)
2007年4月 (92)
文章档案
(7)
2007年7月 (2)
2007年5月 (4)
2007年4月 (1)
相册
Illustration
15ers
jonathan的BLOG
Right There...
宙斯鱼的小鱼缸
小鲍的世界
简单幸福
逃遁的Persephone
阿缪尔的锦瑟
风之语的BLOG
友情链接
(04CS) ljh
(05CS) 小菜虎的窝
(06CS) FreePeter
(06SS) Overboming
(06SS) Sherry
(06SS) 十指飞扬
(06SS) 银色子弹
luohandsome的专栏
平淡是真——啃啃不老阁
收藏夹
[ADN.cn]Library
Debian学习笔记
Dictionary of Algorithms and Data Structures
Gollum
Lex&Yacc
Max On Java
techInterview Discussion
核桃仁
程序员面试题精选100题
铁手
搜索
积分与排名
积分 - 330795
排名 - 168
最新随笔
1. 新博客
2. 慎用xen的make world...
3. 内存模型相关的资料
4. 安全方面的经典论文:A Logic of Authentication
5. Lock-Free 算法的几个链接
6. 10 Papers Every Programmer Should Read
7. PieTTY中按Ctrl+S导致挂起的问题解决
8. Finding and Reproducing Heisenbugs in Concurrent Programs
9. Ubuntu 8.10 浏览网页不稳定的解决方法
10. [zz]苏南经济模式兴衰亲历记
最新评论
1. re: C/C++中的序列点
说的太好了,解决我长久的困扰!
--除美灭日平韩
2. re: 原来GCC是支持尾递归的递推优化的
评论内容较长,点击标题查看
--darkhorse
3. re: Arch下配置samba服务
我按照你的方法,安装了SAMBA,但是 /etc/rc.d/samba start 启动不了samba服务。提示不存在这个文件或目录的,怎么办?
--zhangbear
4. re: [zz]LKM Rootkits on Linux x86 v2.6
rhel 5 系列 安装了 Xen 内核, 怎么rootkit xen kernel 呢?
--消息
5. re: CLRS 习题 16.2-6 部分背包问题的O(n)算法
@ynnej
T(n)=2T(n/2)+O(n) 才是 nlgn 注意其中有一个2
--荒废庭院
阅读排行榜
1. [zz]vim+ctags+taglist插件安装使用(18276)
2. memcpy函数代码分析(9352)
3. [zz]Zotero与Endnote的互相导入(8727)
4. BNF 文法 (1) - 语法树 | 二义性的解决(8187)
5. Java泛型中的? super T语法(6543)
评论排行榜
1. C# 学习笔记 (1)(14)
2. Windows - QQ、网页Flash视频无声音的解决方法(14)
3. URAL 1011(10)
4. 《编程之美》上的一道题目的讨论(8)
5. Singleton模式与双检测锁定(DCL)(7)