咨询电话:024-31891684

pg电玩城-pg电子麻将胡了2试玩|注册|登录 |

javascript中创建新对象-pg电玩城

 

javascript中创建新对象当前位置:pg电玩城-pg电子麻将胡了2试玩>主要服务>网站制作>js/jquery

使用javascript可以创建自己的对象。虽然javascript内部和浏览器本身的功能已十分强大,但javascript还是提供了创建一个新对象的方法。使其不必像超文本标识语言那样,求于或其它多媒体工具,就能完成许多复杂的工作。  本讲介绍了用户自行创建对象的方法, 用户可根据需要创建自己的对象。并介绍了javascript中建数组的方法。
  在javascript中创建一个新的对象是十分简单的。首先它必须定义一个对象,而后再为该对象创建一个实例。这个实例就是一个新对象,它具有对象定义中的基本特征。

一、对象的定义
javascript对象的定义,其基本格式如下:
function object(属性表)
this.prop1=prop1
this.prop2=prop2
...
this.meth=functionname1;
this.meth=functionname2;
...
在一个对象的定义中,可以为该对象指明其属性和方法。通过属性和方法构成了一个对象的实例。如以下是一个关于university对象的定义:
function university(name,city,creatdate url)
this.name=name
this.city=city
this.creatdate=new date(creatdate)
this.url=url
其基本含义如下:
name-指定一个“单位”名称。  
city-“单位”所在城市。  
creatdate-记载university对象的更新日期。  
url-该对象指向一个网址。

二、创建对象实例
一旦对象定义完成后,就可以为该对象创建一个实例了:
newobject=new object();
其中newobjet是新的对象,object已经定义好的对象。例:

u1=new university(“云南省”,“昆明市”,"january 05,199712:00:00","http://www.yn.km")
u2=new university(“云南电子科技大学”,“昆明”,"january 07,1997 12:00:00","htlp://www.ynkj.cn")
 
三、对象方法的使用
在对象中除了使用属性外,有时还需要使用方法。在对象的定义中,我们看到this.meth=functionname语句,那就是为定义对象的方法。实质对象的方法就是一个函数functionname,通过它实现自己的意图。
例在university对象中增加一个方法,该方法是显示它自己本身,并返回相应的字串。
function university(name,city,createdate,url)
this.name=name;
this.city=city;
this.createdate=new date(creatdate);
this.url=url;
this.showuniversity=showuniversity;
其中this.showuniversity就是定义了一个方法---showuniversity()。
而showuniversity()方法是实现university对象本身的显示。
function showuniversity()
for (var prop in this)
alert(prop =" this[prop] "");
其中alert是javascript中的内部函数,显示其字符串。
 
四、javascript中的数组 
使用new创建数组
javascript中没有提供像其它语言具有明显的数组类型,但可以通过function定义一个数组,并使用new对象操作符创建一个具有下标的数组。从而可以实现任何数据类型的存储。
a、定义对象的数组
function arrayname(size){
this.length=size;
for(var x=; x<=size;x )
this[x]=0;
reture this;
}
其中arrayname是定义数组的一个名子,size是有关数组大小的值(1-size),即数组元素的个数。通过for循环对一个当前对象的数组进行定义,最后返回这个数组。
从中可以看出,javascript中的数组是从1到size,这与其它0到size的数组表示方法有所不同,当然你可根据需要将数组的下标由1到size调整到0到size-1,可由下列实现:
function arrayname (size)
for (var x=0; x<=size;x )
this[x]=0;
this.lenght=size;
return this;
从上面可以看出该方法是只是调整了this.lenght的位置,该位置是用于存储数组的大小的。从而调整后的数组的下标将与其它语言一致。但请读者注意正是由于数组下标顺序由1到size,使得javascript中的对象功能更加强大。
b、创建数组实例
一个数组定义完成以后,还不能马上使用,必须为该数组创建一个数组实例:
myarray=new arrayname(n);
并赋于初值:
myarray[1]=“字串1”;
myarray[2]=“字串2”;
myarray[3]=“字串3”;
...
myarray[n]=“字串n”;
一旦给数组赋于了初值后,数组中就具有真正意义的数据了,以后就可以在程序设计过程中直接引用。
创建多维数组
function creatmarray(row,col){
var indx=0;
this.length=(row*10) col
for(var x=1;x<=row;x )
for(var y=1;y<=col;y )
indx=(x*10) y;
this[indx]=””;
}
mymarray=new creatmarray();
之后可通过mymarray[11]、mymarray[12]、mymarray[13]、mymarray[21]、mymarray[22]、mymarray[23]、
…来引用。
内部数组
在java中为了方便内部对象的操作,可以使用窗体(forms)、框架(frames)、元素(element)、链接(links)和锚(anchors)数组实现对象的访问。
 anchors[]:使用《a name=“anchorname“》标识来建立锚的链接。

 links[]: 使用来定义一个越文本链接项。

 forms[]: 在程序中使用多窗体时,建立该数组。

 elements[]:在一个窗口中使用从个元素时,建立该数组。

 frames[]:建立框架时,使用该数组

 anchors[]用于窗体的访问(它是通过《form name=“form1”》所指定的),link[]用于被链接到的锚点的访问(它是通过《a href=url》所指定的)。forms[]反映窗体的属性,而anchors[]反映web页面中的链接属性。
有关锚数组的文档:



 定义第一个锚名
html code
 定义第二个锚名
html code
建立锚的链接
….
该文档段建立了两面全锚的链接,可通过anchors[]访问这些锚。document.anchors[0]反映第一个锚,而document.anchors[1]反映第二个锚的有关信息。
 
五、范例
范例1:一个动态文字滚动的例子。
test5_1.htm




 
with (top.window.location)
{baseurl = href.substring (0,href.lastindexof ("/")   1)}
total_toc_items = 0;
current_overid = "";
last_overid = "";
browser = navigator.appname;
version = parseint(navigator.appversion);
client=null;
loaded = 0;
if (browser == "netscape" && version >= 3) client = "ns3";
function toc_item (img_name,icon_col,width,height) {
if (client =="ns3") {
img_prefix = baseurl   img_name;
this.icon_col = icon_col;
this.toc_img_off = new image (width,height);
this.toc_img_off.src = img_prefix   "_off.gif";
this.toc_img_on = new image (width,height);
this.toc_img_on.src = img_prefix   "_on.gif";
}
}
 
function new_toc_item (img_name,icon_row,width,height) {
toc_item [img_name] = new toc_item (img_name,icon_row,width,height);
}
 
function toc_mouseover (itemid) {
if (client =="ns3") {
current_overid = itemid;
if (current_overid != last_overid) {
document [current_overid].src = toc_item [current_overid].toc_img_on.src;
if (last_overid != "") {
document.images [last_overid].src = toc_item[last_overid].toc_img_off.src;
}
last_overid = current_overid;
}
}
}
 
function toc_mouseout () {
if (client =="ns3") {
if (current_overid != "") {
document.images [current_overid].src = toc_item [current_overid].toc_img_off.src;
}
current_overid = "";
last_overid = "";
}
}
new_toc_item ("1",2,120,20);





 

 

上一条资讯|返回栏目页|下一条资讯

分享到:
0

沈阳网站制作--网站前台效果

易势网站制作,以div css为主,js/jquery为辅,制作利于优化,页面美观的优质网站!

top

网络策划公司|新浪官方微博|

网站软件咨询

客户服务咨询

网站地图