【文章內(nèi)容簡(jiǎn)介】
alue=FindLauraonclick=findEmployeeById(8)。 現(xiàn)在我們?cè)傧乱怀?,看看如何過濾一個(gè)Array中的元素,從每個(gè)元素中得到我們想要的成員。 script functionshowLocalLinks(paragraph){ paragraph=$(paragraph)。 varlinks=$A((39。a39。))。 //findlinksthatdonotstartwith39。39。 varlocalLinks=(function(link){ varstart=(0,4)。 returnstart!=39。39。 })。 //nowthelinktexts vartexts=(39。innerHTML39。)。 //gettheminasinglestring varresult=()。 alert(result)。 } /script pid=someText Thisahref=text/ahas aahref=localAnchorlot/aof ahref=otherAnchorlinks/a.Someare ahref=external/a andsomeareahref=someAnchorlocal/a /p inputtype=buttonvalue=FindLocalLinksonclick=showLocalLinks(39。someText39。) 上面的代碼僅僅是一點(diǎn)小小的實(shí)踐讓人愛上這種語(yǔ)法。請(qǐng)參看 Enumerable和Array的所有函數(shù)五: JavaScript類擴(kuò)展 類庫(kù)實(shí)現(xiàn)強(qiáng)大功能的一種途徑是擴(kuò)展已有的JavaScript 類。 對(duì) Object的擴(kuò)展MethodKindArgumentsDescriptionextend(destination, source)staticdestination: any object, source: any object提供一種通過拷貝所有源以象屬性和函數(shù)到目標(biāo)函數(shù)實(shí)現(xiàn)繼承的方法inspect(targetObj)statictargetObj: any object返回可讀性好關(guān)于目標(biāo)對(duì)象的文字描述,如果對(duì)象實(shí)例沒有定義一個(gè)inspect函數(shù),默認(rèn)返回toString函數(shù)的值。 對(duì)Number的擴(kuò)展MethodKindArgumentsDescriptiontoColorPart()instance(none)返回?cái)?shù)字的十六進(jìn)制表示形式。在把一個(gè)RGB數(shù)字轉(zhuǎn)換成HTML表現(xiàn)形式時(shí)很有用。succ()instance(none)返回下一個(gè)數(shù)字,這個(gè)方法可用于迭代調(diào)用場(chǎng)景中。 times(iterator)instanceiterator: a function object conforming to Function(index)Calls the iterator function repeatedly passing the current index in the index argument. 反復(fù)調(diào)用iterator函數(shù)并傳遞當(dāng)前index到iterator的index參數(shù)。 下面的例子用提示框顯示09。 script functiondemoTimes(){ varn=10。 (function(index){ alert(index)。 })。 /*************************** *youcouldhavealsoused: *(10).times(....)。 ***************************/ } /script inputtype=buttonvalue=Test()onclick=demoTimes() 對(duì) Function擴(kuò)展MethodKindArgumentsDescriptionbind(object)instanceobject: the object that owns the method返回function的實(shí)例,這個(gè)實(shí)例和源function的結(jié)構(gòu)一樣,但是它已被綁定給了參數(shù)中提供的object,就是說,function中的this指針指向參數(shù)object。bindAsEventListener(object)instanceobject: the object that owns the method用法和上面的bind一樣,區(qū)別在于用來綁定事件。讓我們看看如何運(yùn)用這些擴(kuò)展。 inputtype=checkboxid=myChkvalue=1Test? script //declaringtheclass varCheckboxWatcher=()。 //definingtherestoftheclassimplementation ={ initialize:function(chkBox,message){ =$(chkBox)。 =message。 //assigningourmethodtotheevent = (this)。 }, showMessage:function(evt){ alert(+39。(39。++39。)39。)。 } }。 varwatcher=newCheckboxWatcher(39。myChk39。,39。Changed39。)。 /script 對(duì)String的擴(kuò)展MethodKindArgumentsDescriptionstripTags()instance(none)返回一個(gè)把所有的HTML或XML標(biāo)記都移除的字符串。stripScripts()instance(none)返回一個(gè)把所有的script都移除的字符串。escapeHTML()instance(none)返回一個(gè)把所有的HTML標(biāo)記合適的轉(zhuǎn)義掉的字符串。unescapeHTML()instance(none)escapeHTML()的反轉(zhuǎn)。extractScripts()instance(none)返回一個(gè)包含在string中找到的所有script的數(shù)組。evalScripts()instance(none)執(zhí)行在string中找到的所有script。toQueryParams()instance(none)把querystring分割才一個(gè)用parameter name做index的聯(lián)合Array,更像一個(gè)hash。parseQuery()instance(none)和toQueryParams()一樣.toArray()instance(none)把字符串轉(zhuǎn)換成字符數(shù)組.camelize()instance(none)轉(zhuǎn)換一個(gè)以連字符連接的字符串成一個(gè)駱駝法樣式的字符串。比如,這個(gè)函數(shù)在寫代碼時(shí),把它做為一個(gè)樣式工具使用是很有用的。 對(duì) Array的擴(kuò)展因?yàn)閍rray擴(kuò)展于enumerable,所以所有enumberable對(duì)象的函數(shù),array都是可以使用的,除此之外,下面的這些也是已經(jīng)實(shí)現(xiàn)了的。 MethodKindArgumentsDescriptionclear()instance(none)清空。pact()instance(none)返回一個(gè)不包括源array中null或undefined元素的array,此方法不改變?cè)碼rray。first()instance(none)返回array的第一個(gè)對(duì)象。flatten()instance(none)通過遞歸組合array每個(gè)元素的子元素(如果該元素也是array)來返回一個(gè)“扁平的”一維的array。indexOf(value)instancevalue: what you are looking for.返回給出數(shù)字位置(從0算起)的元素,如果在該位置沒有找到對(duì)象,返回1。 inspect()instance(none)重載inspect(),返回更好格式的反映Array每個(gè)元素的字符描述。last()instance(none)返回最后一個(gè)元素。reverse([applyToSelf])instanceapplyToSelf: indicates if the array itself should also be reversed.反轉(zhuǎn)Array中元素的順序,如果沒有給出參數(shù),或參數(shù)為true,則源Array中元素的順序也反轉(zhuǎn),否則源Array保持不變。 shift()instance(none)返回Array的第一個(gè)元素并從Array中移除它,Array的Length1。without(value1 [, value2 [, .. valueN]])instancevalue1 ... valueN: values to be excluded if present in the array.返回一個(gè)把參數(shù)列表中包含的元素從源Array中排除的Array。 document DOM擴(kuò)展MethodKindArgumentsDescriptiongetElementsByClassName(className [, parentElement])instanceclassName: name of a CSS class associated with the elements, parentElement: object or id of the element that contains the elements being retrieved.返回所有CSS className屬性等于className參數(shù)的元素,如果沒有給出parentElement,那么將搜索document body。(,因?yàn)橛袝r(shí)有的頁(yè)面沒有body) Event擴(kuò)展PropertyTypeDescriptionKEY_BACKSPACENumberNumber8: Constant. Code for the Backspace key.KEY_TABNumber9: Constant. Code for the Tab key.KEY_RETURNNumber13: Constant. Code for the Return key.KEY_ESCNumber27: Constant. Code for the Esc key.KEY_LEFTNumber37: Constant. Code for the Left arrow key.KEY_UPNumber38: Constant. Code for the Up arrow key.KEY_RIGHTNumber39: Constant. Code for the Right arrow key.KEY_DOWNNumber40: Constant. Code for the Down arrow key.KEY_DELETENumber46: Constant. Code for the Delete key.observers:ArrayList of cached observers. Part of the internal implementation details of the object.MethodKindArgumentsDescriptionelement(event)staticevent: an Event object返回事件源對(duì)象。isLeftClick(event)staticevent: an Event object如果點(diǎn)擊了鼠標(biāo)左鍵,返回true.pointerX(event)staticevent: an Event object返回鼠標(biāo)的X座標(biāo)。 pointerY(event)staticevent: an Event object返回鼠標(biāo)的Y座標(biāo)。stop(event)staticevent: an Event object使用此函數(shù)來中斷事件的默認(rèn)行為并阻止傳遞(冒泡)。findElement(event, tagName)staticevent: an Event object, tagName: name of the desired tag.從事件源對(duì)象開始向上搜索DOM樹,直到找到第一個(gè)符合tagName的元素observe(element, name, observer, useCapture)staticelement: object or id, name: event name (like 39。click39。, 39。load39。, etc), observer: function to handle the event, useCapture: if true, handles the event in the capture phase and if false in the bubbling phase.為對(duì)象的某個(gè)事件增加一個(gè)處理函數(shù)。s