【文章內(nèi)容簡介】
alue=FindLauraonclick=findEmployeeById(8)。 現(xiàn)在我們再下一城,看看如何過濾一個Array中的元素,從每個元素中得到我們想要的成員。 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。) 上面的代碼僅僅是一點小小的實踐讓人愛上這種語法。請參看 Enumerable和Array的所有函數(shù)五: JavaScript類擴展 類庫實現(xiàn)強大功能的一種途徑是擴展已有的JavaScript 類。 對 Object的擴展MethodKindArgumentsDescriptionextend(destination, source)staticdestination: any object, source: any object提供一種通過拷貝所有源以象屬性和函數(shù)到目標函數(shù)實現(xiàn)繼承的方法inspect(targetObj)statictargetObj: any object返回可讀性好關(guān)于目標對象的文字描述,如果對象實例沒有定義一個inspect函數(shù),默認返回toString函數(shù)的值。 對Number的擴展MethodKindArgumentsDescriptiontoColorPart()instance(none)返回數(shù)字的十六進制表示形式。在把一個RGB數(shù)字轉(zhuǎn)換成HTML表現(xiàn)形式時很有用。succ()instance(none)返回下一個數(shù)字,這個方法可用于迭代調(diào)用場景中。 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ù)并傳遞當前index到iterator的index參數(shù)。 下面的例子用提示框顯示09。 script functiondemoTimes(){ varn=10。 (function(index){ alert(index)。 })。 /*************************** *youcouldhavealsoused: *(10).times(....)。 ***************************/ } /script inputtype=buttonvalue=Test()onclick=demoTimes() 對 Function擴展MethodKindArgumentsDescriptionbind(object)instanceobject: the object that owns the method返回function的實例,這個實例和源function的結(jié)構(gòu)一樣,但是它已被綁定給了參數(shù)中提供的object,就是說,function中的this指針指向參數(shù)object。bindAsEventListener(object)instanceobject: the object that owns the method用法和上面的bind一樣,區(qū)別在于用來綁定事件。讓我們看看如何運用這些擴展。 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 對String的擴展MethodKindArgumentsDescriptionstripTags()instance(none)返回一個把所有的HTML或XML標記都移除的字符串。stripScripts()instance(none)返回一個把所有的script都移除的字符串。escapeHTML()instance(none)返回一個把所有的HTML標記合適的轉(zhuǎn)義掉的字符串。unescapeHTML()instance(none)escapeHTML()的反轉(zhuǎn)。extractScripts()instance(none)返回一個包含在string中找到的所有script的數(shù)組。evalScripts()instance(none)執(zhí)行在string中找到的所有script。toQueryParams()instance(none)把querystring分割才一個用parameter name做index的聯(lián)合Array,更像一個hash。parseQuery()instance(none)和toQueryParams()一樣.toArray()instance(none)把字符串轉(zhuǎn)換成字符數(shù)組.camelize()instance(none)轉(zhuǎn)換一個以連字符連接的字符串成一個駱駝法樣式的字符串。比如,這個函數(shù)在寫代碼時,把它做為一個樣式工具使用是很有用的。 對 Array的擴展因為array擴展于enumerable,所以所有enumberable對象的函數(shù),array都是可以使用的,除此之外,下面的這些也是已經(jīng)實現(xiàn)了的。 MethodKindArgumentsDescriptionclear()instance(none)清空。pact()instance(none)返回一個不包括源array中null或undefined元素的array,此方法不改變源array。first()instance(none)返回array的第一個對象。flatten()instance(none)通過遞歸組合array每個元素的子元素(如果該元素也是array)來返回一個“扁平的”一維的array。indexOf(value)instancevalue: what you are looking for.返回給出數(shù)字位置(從0算起)的元素,如果在該位置沒有找到對象,返回1。 inspect()instance(none)重載inspect(),返回更好格式的反映Array每個元素的字符描述。last()instance(none)返回最后一個元素。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的第一個元素并從Array中移除它,Array的Length1。without(value1 [, value2 [, .. valueN]])instancevalue1 ... valueN: values to be excluded if present in the array.返回一個把參數(shù)列表中包含的元素從源Array中排除的Array。 document DOM擴展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。(,因為有時有的頁面沒有body) Event擴展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返回事件源對象。isLeftClick(event)staticevent: an Event object如果點擊了鼠標左鍵,返回true.pointerX(event)staticevent: an Event object返回鼠標的X座標。 pointerY(event)staticevent: an Event object返回鼠標的Y座標。stop(event)staticevent: an Event object使用此函數(shù)來中斷事件的默認行為并阻止傳遞(冒泡)。findElement(event, tagName)staticevent: an Event object, tagName: name of the desired tag.從事件源對象開始向上搜索DOM樹,直到找到第一個符合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.為對象的某個事件增加一個處理函數(shù)。s