JavaScript教程--从入门到精通(5)
JavaScript教程--从入门到精通(5)

在JavaScript中创建新对象
使用JavaScript可以创建自己的对象 虽然JavaScript内部和浏览器本身的功能已十分强大 但JavaScript还是提供了创建一个新对象的方法 使其不必像超文本标识语言那样 求于或其它多媒体工具 就能完成许多复杂的工作 在JavaScript中创建一个新的对象是十分简单的 首先它必须定义一个对象 而后再为该对象创建一个实例 这个实例就是一个新对象 它具有对象定义中的基本特征
一 对象的定义JavaScript对象的定义 其基本格式如下 Function Object(属性表)This prop =prop This prop =prop th=FunctionName ;th=FunctionName ; 在一个对象的定义中 可以为该对象指明其属性和方法 通过属性和方法构成了一个对象的实例 如以下是一个关于University对象的定义 Function university(name city creatDate URL)This name=nameThis city=cityThis creatDate=New Date(creatDate)This URL=URL其基本含义如下 Name-指定一个 单位 名称 City- 单位 所在城市 CreatDate-记载university对象的更新日期 URL-该对象指向一个网址
二 创建对象实例一旦对象定义完成后 就可以为该对象创建一个实例了 NewObject=New object();其中Newobjet是新的对象 Object已经定义好的对象 例 U =New university( 云南省 昆明市 January : : )U =New university( 云南电子科技大学 昆明 January : : htlp:// YNKJ CN ) 三 对象方法的使用在对象中除了使用属性外 有时还需要使用方法 在对象的定义中 我们看到th=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]= ;Reture this;}其中arrayName是定义数组的一个名子 Size是有关数组大小的值( size) 即数组元素的个数 通过for循环对一个当前对象的数组进行定义 最后返回这个数组 从中可以看出 JavaScript中的数组是从 到size 这与其它0到size的数组表示方法有所不同 当然你可根据需要将数组的下标由1到size调整到0到size-1 可由下列实现:Function arrayName (size)For (var X= ; X<=size;X++)this[X]= ;this lenght=size;Return this;从上面可以看出该方法是只是调整了this lenght的位置 该位置是用于存储数组的大小的 从而调整后的数组的下标将与其它语言一致 但请读者注意正是由于数组下标顺序由1到size 使得JavaScript中的对象功能更加强大 b 创建数组实例一个数组定义完成以后 还不能马上使用 必须为该数组创建一个数组实例 Myarray=New arrayName(n);并赋于初值 Myarray[ ]= 字串1 Myarray[ ]= 字串2 Myarray[ ]= 字串3 Myarray[n]= 字串n 一旦给数组赋于了初值后 数组中就具有真正意义的数据了 以后就可以在程序设计过程中直接引用 创建多维数组Function creatMArray(row col){var indx= ;this length=(row* )+colfor(var x= ;x<=row;x++)for(var y= ;y<=col;y++)indx=(x* )+y;this[indx]= ;}myMArray=new creatMArray();之后可通过myMArray[ ] myMArray[ ] myMArray[ ] myMArray[ ] myMArray[ ] myMArray[ ] …来引用 内部数组在Java中为了方便内部对象的操作 可以使用窗体(Forms) 框架(Frames) 元素(element) 链接(links)和锚(Anchors)数组实现对象的访问 anchors[]:使用《A name= anchorName 》标识来建立锚的链接links[]: 使用<A href= URL >来定义一个越文本链接项
Forms[]: 在程序中使用多窗体时 建立该数组
Elements[]:在一个窗口中使用从个元素时 建立该数组
Frames[]:建立框架时 使用该数组
五 范例范例 一个动态文字滚动的例子 <><head><title></title><script LANGUAGE= JavaScript > with (top window location){baseURL = href substring ( href lastIndexOf ( / ) + )}total_toc_items = ;current_overID = ;last_overID = ;browser = navigator appName;version = parseInt(navigator appVersion);client=null;loaded = ;if (browser == Netscape && version >= ) client = ns ;function toc_item (img_name icon_col width height) {if (client == ns ) {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 == ns ) {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 == ns ) {if (current_overID != ) {document images [current_overID] src = toc_item [current_overID] toc_img_off src;}current_overID = ;last_overID = ;}}new_toc_item ( );<! Beginfunction bannerObject(p){this msg = MESSAGEthis out = this pos = POSITIONthis delay = DELAYthis i = this reset = clearmessage} function clearmessage(){this pos = POSITION}var POSITION = ;var DELAY = ;var MESSAGE = 这是一个动态JavaScript文字显示的例子 ;var scroll = new bannerObject();function scroller(){scroll out += ;if(scroll pos> )for (scroll i = ; scroll i < scroll pos; scroll i++) { scroll out += ; }if (scroll pos>= )scroll out += scroll msgelsescroll out = scroll msg substring( scroll pos scroll msg length)document noticeForm notice value = scroll outscroll out = ;scroll pos ;scroll pos ;if (scroll pos < (scroll msg length)) { scroll reset(); } setTimeout( scroller() scroll delay);}</script></head><body onload= scroller() bgcolor= # link= #C C C vlink= #C C C alink= # text= #C C C ><table border= cellspacing= cellpadding= ><tr><td width= % ><form NAME= noticeForm ><p><input TYPE= text name= notice size= ></p></form></td></tr></table></center></div></body></> 范例 :颜色变化的例子 <><head><script><! function makearray(n) {this length = n;for(var i = ; i <= n; i++)this[i] = ;return this;}hexa = new makearray( );for(var i = ; i < ; i++)hexa[i] = i;hexa[ ]= a ;hexa[ ]= b ;hexa[ ]= c ;hexa[ ]= d ;hexa[ ]= e ;hexa[ ]= f ;function hex(i) {if (i < )return ;else if (i > )return ff ;else return + hexa[Math floor(i/ )] + hexa[i% ];}function setbgColor(r g b) {var hr = hex(r);var hg = hex(g);var hb = hex(b);document bgColor = # +hr+hg+hb;}function fade(sr sg sb er eg eb step) {for(var i = ; i <= step; i++) {setbgColor( Math floor(sr * ((step i)/step) + er * (i/step)) Math floor(sg * ((step i)/step) + eg * (i/step)) Math floor(sb *((step i)/step) + eb * (i/step))); }}function fadein() {fade( );fade( );fade( );}fadein();// ></script><body></body></> 本讲介绍了用户自行创建对象的方法 用户可根据需要创建自己的对象 并介绍了JavaScript中建数组的方法 lishixinzhi/Article/program/Java/Javascript/201311/25351