您的位置:首页 > 网页制作 > Javascript

Javascript里的类构造

日期:2006-09-12 14:38:44 点击: 来自: 作者:

Javascript里的类构造;以前不是很常用到,Javascript1.5有更多特性,可惜IE不支持。
自定义类:
<script language="javascript1.1">
//constructor;
function Alerter(aMsg,aTimes)
{
//properties;
this.mMsg=aMsg;
this.mTimes=aTimes;
//methods;
this.start=AlerterStart;
}

function AlerterStart()
{
var i;
for(i=0;i<this .mTimes;i++)
{
alert(this.mMsg+" time:"+(i+1));
}
return;
}

//instances;

var objAlerter=new Alerter("funny",5);

document.write("<a href="javascript:objAlerter.start();">TEST GO!");
</script>

添加或重写固有类的方法/属性:

<script language="javascript1.1">
//Array;
function Array_sum()
{
var t=0;
for(var i=0;i<this .length;i++)
{
t+=this[i];
}
return t;
}

//add a method;

Array.prototype.sum=Array_sum;

var a=new Array(1,2,3,4,5);

document.write("<a href="javascript:alert(a.sum());">TEST GO!");
</script>
 

More..素材图片 Picture Navigation
Javascript热门 Class Hot
Javascript推荐 Class Commend
版权所有:中国网站资源 2005- 未经授权禁止复制或建立镜像 This Site Tech:XHTML+DIV+CSS+Javascript
CopyRight ® 2005- www.chinaddv.com online services. all rights reserved. ICP06016627
Optimized to 1024x768 to Firefox,Netscape,Opera,MS-IE6+.