Snowdream
I'm awake but my world is half asleep
posts - 398, comments - 234, trackbacks - 0, articles - 7
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
JavaScript 学习 - Inheritance
Posted on 2007-05-05 18:57
ZelluX
阅读(109)
评论(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新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-05-05 20:37 编辑过
相关文章:
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
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
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
公告
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(10)
给我留言
查看公开留言
查看私人留言
随笔分类
(398)
Algorithm(57)
C/C++(40)
Courses(21)
Economics(2)
Laboratory(26)
Linux(46)
Mathematics(12)
OOP(89)
Scripting(19)
Security(3)
System(29)
Web(10)
书、电影、音乐(11)
其他(14)
点滴(19)
随笔档案
(386)
2008年11月 (1)
2008年10月 (9)
2008年9月 (1)
2008年7月 (2)
2008年6月 (6)
2008年5月 (13)
2008年4月 (19)
2008年3月 (8)
2008年2月 (33)
2008年1月 (19)
2007年12月 (10)
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题
铁手
搜索
积分与排名
积分 - 119336
排名 - 71
最新随笔
1. 为什么Python中的self必须保留?
2. Most Influential PLDI Paper Award
3. [zz]Zotero与Endnote的互相导入
4. Python中inner function的binding处理
5. Erlang的Hello World: 一个计数程序
6. Xen Notes [1]
7. Erlang的GeSHi语法高亮文件
8. 32款最好的编程字体
9. 几个并行计算、内核相关的链接
10. OS Lab 零散记录
最新评论
1. re: Windows - QQ、网页Flash视频无声音的解决方法
那个字符串我有的啊,还是没用
--宝
2. re: Windows - QQ、网页Flash视频无声音的解决方法[未登录]
水平高!!谢谢!!
之前搞了N次,化了N的时间。。。
搞了几个月
今天终于搞好了,谢谢!!
--竹笋
3. re: Windows - QQ、网页Flash视频无声音的解决方法[未登录]
水平高!!谢谢!!
之前搞了N次,化了N的时间。。。
几个月的同样问题终于解决!
--竹笋
4. re: Xen Notes [1]
@rubisva
就是加一行PAE=yes呀
--ZelluX
5. re: Xen Notes [1]
评论内容较长,点击标题查看
--rubisva
阅读排行榜
1. memcpy函数代码分析(2925)
2. 水源上看到的腾讯笔试题(2628)
3. [zz]vim+ctags+taglist插件安装使用(2130)
4. 《编程之美》上的一道题目的讨论(2070)
5. 最近读的两篇paper(1850)
评论排行榜
1. C# 学习笔记 (1)(14)
2. URAL 1011(10)
3. 《编程之美》上的一道题目的讨论(8)
4. Singleton模式与双检测锁定(DCL)(7)
5. 肆意一把(7)