JavaScript的Prototype使用
来源:互联网 发布:linux上传文件的命令 编辑:程序博客网 时间:2024/04/28 12:32
作者: xuanner 发布日期: 2007-8-23 20:54:41 Email:chenxuan5627@163.com
---------------------------------------------------------------------------------------------------------------
(原创作品,转载请保留作者著作信息)
(1)动态增加对象的属性
语法:类名.prototype.属性名 = 值;
(2)动态增加对象的方法
语法:类名.prototype.方法名 = 已声明的方法;
(3)实现对象的继承
语法:子类.prototype = new 父类();
例子:
<html>
<head>
<title>JavaScript中类的继承及动态增加属性和方法</title>
<script language="JavaScript">
<!--
//父类
function fatherClass(x, y, color) ...{
this.x = x;
this.y = y;
this.color = color;
}
//子类
function subClass(r) ...{
this.r = r;
this.info = showInfo;
}
//子类的方法
function showInfo() ...{
var s = this.PI * this.r * this.r; //使用PI
document.writeln("半径:"+this.r+"<br>");
document.writeln("面积:" + s+"<br>");
document.writeln("X坐标:"+this.x+"<br>");
document.writeln("Y坐标:"+this.y+"<br>");
document.writeln("颜色:"+this.color+"<br>");
this.myMethod(); //调用myMethod方法
}
function dynamicMethod() ...{
document.writeln("动态增加了一个方法"+"<br>");
}
//继承父类
subClass.prototype = new fatherClass(); //使subClass继承fatherClass
//-->
</script>
</head>
<body>
<script language="JavaScript">
var sc = new subClass(5);
subClass.prototype.PI = 3.142; //动态增加一个属性PI
subClass.prototype.myMethod = dynamicMethod; //动态增加一个方法myMethod
with(sc) ...{
x = 100;
y = 200;
color = "red";
}
sc.info();
</script>
</body>
</html>
<head>
<title>JavaScript中类的继承及动态增加属性和方法</title>
<script language="JavaScript">
<!--
//父类
function fatherClass(x, y, color) ...{
this.x = x;
this.y = y;
this.color = color;
}
//子类
function subClass(r) ...{
this.r = r;
this.info = showInfo;
}
//子类的方法
function showInfo() ...{
var s = this.PI * this.r * this.r; //使用PI
document.writeln("半径:"+this.r+"<br>");
document.writeln("面积:" + s+"<br>");
document.writeln("X坐标:"+this.x+"<br>");
document.writeln("Y坐标:"+this.y+"<br>");
document.writeln("颜色:"+this.color+"<br>");
this.myMethod(); //调用myMethod方法
}
function dynamicMethod() ...{
document.writeln("动态增加了一个方法"+"<br>");
}
//继承父类
subClass.prototype = new fatherClass(); //使subClass继承fatherClass
//-->
</script>
</head>
<body>
<script language="JavaScript">
var sc = new subClass(5);
subClass.prototype.PI = 3.142; //动态增加一个属性PI
subClass.prototype.myMethod = dynamicMethod; //动态增加一个方法myMethod
with(sc) ...{
x = 100;
y = 200;
color = "red";
}
sc.info();
</script>
</body>
</html>
- JavaScript的Prototype使用
- Javascript prototype的简单使用
- 使用prototype扩展的JavaScript常用函数库
- javascript的prototype经典使用场景
- javascript的prototype经典使用场景
- JavaScript中的关键字之prototype的使用
- JavaScript中prototype和proto的使用
- javascript prototype 使用
- javascript prototype 使用(转)
- JavaScript prototype 使用介绍
- JavaScript prototype 使用介绍
- JavaScript prototype 使用介绍
- JavaScript prototype 使用介绍
- JavaScript prototype 使用介绍
- JavaScript prototype 使用介绍
- JavaScript的prototype是什么
- javascript的prototype继承
- JavaScript的Prototype对象
- Win XP十大必禁服务以及其它问题
- 教您给Windows目录加口令的特殊方法
- javamail 快速入门
- 两’高‘文件系统reiserfs的救赎??
- 股市K线图战法(图文对照讲解)
- JavaScript的Prototype使用
- 用于排列组合的类
- 好久没来了
- 另类方法 四种Windows神奇开机方法
- 编写图形软件的一点心得
- jsp安全之我见
- java反射实例
- 实习结束了,临走还捞了一票...和Jason聊了许多关于以后在校园技术实践以及学习的方法,宝贵经验,记录下来。
- 个人简介