ࡱ> `b_[ 3bjbj 4hΐΐ+  QQQQQeeee 4eU=L, $3!.QQQ   QQ   HpP)Den`%0Uh"("pp"Q<  U"  : The Array Object http://www.slis.indiana.edu/faculty/hrosenba/www/Demo/javascript2/arrays.html Arrays are data structures that store information in a set of adjacent memory addresses. In practice, this means is that you can store other variables and objects inside an array and can retrieve them from the array by referring to their position number in the array. Each variable or object in an array is called an element. Unlike stricter languages, such as Java, you can store a mixture of data types in a single array. For example, you could have array with the following four elements: an integer, a window object, a string and a button object. In addition, JavaScript arrays are always expandable -- meaning that the number of elements in a JavaScript array is not constrained after the array is first declared or initialized. Why should you learn about arrays? Here are three good reasons: They can greatly simplify your programming [HYPERLINK "file:///C:\\Documents%20and%20Settings\\ibserveis.com\\Escritorio\\CSS-XML%202011\\Javascript_%20Arrays.htm" \l "simplify"how?]; The DOM stores objects as arrays and you can reference them as elements in an array [HYPERLINK "file:///C:\\Documents%20and%20Settings\\ibserveis.com\\Escritorio\\CSS-XML%202011\\Javascript_%20Arrays.htm" \l "DOMarrays"how?]; You can store small tables of information in them [HYPERLINK "file:///C:\\Documents%20and%20Settings\\ibserveis.com\\Escritorio\\CSS-XML%202011\\Javascript_%20Arrays.htm" \l "tables"how?]. The details of each of these points will be explained below. To reference an element in an array, one appends square brackets to the end of the array name. For example, let us name the array mentioned above "arrayList". It's structure would look like this: arrayList[0] = 7 // an integer arrayList[1] = newWind //a window object ref arrayList[2] = "blue suede shoes" // a string arrayList[3] = input // a button reference (where input = newWind.document.formName.buttonName) The notation arrayList[1] is read "arrayList sub 1". The position number of an element in an array is called the index number. Note that the first element in an array has index number 0, not 1. This is called zero-based indexing and can be confusing to novice programmers. The index number of an element is one less than it's position. For example, the third element of arrayList has index number 2. Thus, the string "blue suede shoes" is the third element in arrayList. Forgetting this can lead to what are called "off-by-one errors". Creating an Array In JavaScript, arrays are objects and they can be constructed in one of three ways. 1. JavaScript allows you to instantiate an array using the "new" operator, as follows: arrayList = new Array(4) This creates a new array called "arrayList" with 4 empty elements. Note that by declaring an array this way, you are not limited to only 4 elements--you can always expand the array. Specifying the number of elements is optional, so you could also write: arrayList = new Array() To define the elements of the array, you would then specify the elements. For example to make an array of book titles (which would be string objects in JavaScript), you might write: books = new Array(5) books[0] = "Jane Eyre" books[1] = "The Stranger" books[2] = "Oliver Twist" books[3] = "Rebecca" books[4] = "Stranger in a Strange Land" You could also add: books[82] = "The Mote in God's Eye" This is allowable in JavaScript. By doing this, you would have expanded the array to have 83 elements and elements 5 through 81 would be empty (null) elements. This method is compatible with all versions of JavaScript. 2. The second way is to supply the elements as parameters to the array constructor: books = new Array("Jane Eyre","The Plague","Oliver Twist","Rebecca","Stranger in a Strange Land") Note that there are no spaces between the parameters. For long arrays, I do not recommend using this method due to the inability to write single line statements over many lines. (In JavaScript, unlike most other programming languages, a carriage return (newline) is considered the end of the programming statement. Most other (C-based) languages mark the end of programming statement with a semi-colon.) 3. In JavaScript 1.2 environments, you can also create an array using brackets to specify the elements to be put in the array: arrayList = [ 2, 4, 15, 'a', "once upon a time" ] This statement creates an array called arrayList with 5 elements. No "new" constructor statement is required. The first three elements are integers, the fourth is a character and the final element is a string. Thus, the value of arrayList[3] is the character 'a'. The Virtues of Using Arrays Arrays simplify your programs Suppose you wrote a web page asking users what their favorite book was. In response to their answer, you would like to respond with: "That is one of my favorite books too!" or "That one is not on my list of favorites." Here is one way you could check the user's input against your list of favorite books: var input = document.formName.textFieldName var fav1 = "Jane Eyre" var fav2 = "The Stranger" var fav3 = "Oliver Twist" var fav4 = "Rebecca" var fav5 = "Stranger in a Strange Land" if ( fav1 == input ) { document.write("That is one of my favorite books too!"); } else if ( fav2 == input ) { document.write("That is one of my favorite books too!"); } else if ( fav3 == input ) { document.write("That is one of my favorite books too!"); } else if ( fav4 == input ) { document.write("That is one of my favorite books too!"); } else if ( fav5 == input ) { document.write("That is one of my favorite books too!"); } else { document.write("That one is not on my list of favorites."); } This method is very space wasteful and repetitive. Imagine how long and ugly it would be if you had 15 favorite books. Or 30. An alternative way to write this script would be: if ( fav1 == input || fav2 == input || fav3 == input || fav4 == input || fav5 == input ) { document.write("That is one of my favorite books too!"); } else { document.write("That one is not on my list of favorites."); } By using the Boolean OR operator, ||, this method saves a considerable amount of space over the last one. However, it still becomes bulky quite quickly as the number of variables you want to test increases. In addition, this method runs into the problem of not being able to write single code phrases over multiple lines in JavaScript. (Meaning that long lines must be written on a single line--making them hard to see and a possible cause of error in some Unix text editors.) The right way to write this script is with arrays. First you would define an array with the list of your favorite books: books = new Array(5) books[0] = "Jane Eyre" books[1] = "The Stranger" books[2] = "Oliver Twist" books[3] = "Rebecca" books[4] = "Stranger in a Strange Land" Then you use a for loop to iterate over each element of the array and compare it to the user's input. The length property of the Array object returns the number of elements in the array and can be used to create the conditional statement in a for loop: for (var i = 0; i < books.length; i++) { if (books[i] == input) { document.write("That is one of my favorite books too!"); break; } } //end for loop if (i == books.length) { document.write("That one is not on my list of favorites.") } The HYPERLINK "http://developer.netscape.com/docs/manuals/communicator/jsref/stmt7.htm" \l "1004804"for loop iterates over each element of the array "books". As it does so, it checks each element to see if it matches the user's input. If it matches, it tells the user with a document.write() command and then uses a break command to jump out of the for loop. break is an inbuilt JavaScript command that causes the program to jump out of a repetition loop--that is, a for loop or a while loop. Without the break statement, the for loop would cycle through all five books, even if the an earlier book had matched the user's input. Put another way, without the break statement, the variable i would be equal to 5 when the for loop was finished executing. In that case, the if statement after the for loop would evaluate to true. With the break statment, if the user input matches, say, books[1], then when the for loop was finished executing i would be equal to 1. A break statement causes the flow of program execution to immediately leave the repetition loop that contains it. The first statement after the closing bracket of the for loop is the next thing to be executed. In this case, the if statement following the for loop would be tested and it would evaluate to false. The DOM stores objects as arrays and you can reference them as elements in an array Many objects in the DOM have elements that can occur multiple times. For example, a window can have multiple frames. A document can have multiple forms or multiple images. A form can have multiple "widgets", which in the DOM are called "elements". To help keep track of them all, the DOM automatically constructs an array with a reference to each element for each object that can have multiple elements. This array is stored as a property of the parent element and it can be referenced like any other property: with dot syntax. Suppose you load into your browser a document that has 3 images. "images[i]" is a property of the document object. Thus the first image (first here means first to occur in the html source code) can be referenced as: document.images[0] Here are a few of the arrays elements in the DOM and their parent containers: Window object frames[i] Document object applets[i] forms[i] images[i] layers[i] links[i] Form object elements[i] Web- Marc de fotos Marco de fotos
&stc r ݾ_?((,hKhKOJPJQJaJmH nH sH tH ?hKhK5B*OJPJQJ\^JaJmH nH phsH tH ?hKhK6B*OJPJQJ]^JaJmH nH phsH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH hfhKCJmH nH sH tH hfhfCJmH nH sH tH =hf5B*CJOJPJQJ\^JaJmH nH phsH tH ChKhK5B*CJOJPJQJ\^JaJmH nH phsH tH t ] ] C C  B/C./ & Fddd[$\$gdKddd[$\$gdK dgdK$ddd@&[$\$a$gdK Z ] S T U Y Z \ ] 9 Ȩȑȑqi^Si3iqqi^>hKhK5>*B*CJOJPJQJ\mH nH phsH tH jhxhUhOh(FmH sH jh(FU?hKhK5B*CJOJPJQJ\aJmH nH phsH tH ,hKhKOJPJQJaJmH nH sH tH ?hKhK5B*OJPJQJ\^JaJmH nH phsH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH 3hV.B*OJPJQJ^JaJmH nH phsH tH 9 : ; ? @ B C v w @C  쬕쬕bbI0hKhKOJPJQJ^JaJmH nH sH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH j,hxhUhOh(FmH sH ,hKhKOJPJQJaJmH nH sH tH ?hKhK5B*CJOJPJQJ\aJmH nH phsH tH >hKhK5>*B*CJOJPJQJ\mH nH phsH tH jh(FUjhxhU 47gj  &@B~ɗ{{^A^^9hKhKB*CJOJPJQJ^JmH nH phsH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH 6hKhK6CJOJPJQJ]^JmH nH sH tH 0hKhKOJPJQJ^JaJmH nH sH tH 0hKhKCJOJPJQJ^JmH nH sH tH ,hKhKOJPJQJaJmH nH sH tH =hKhKB*CJOJPJQJ^JaJmH nH phsH tH ~)/@C  '-/«««j«jT;0hKhKOJPJQJ^JaJmH nH sH tH *h*OJPJQJ^JaJmH nH sH tH =hKhKB*CJOJPJQJ^JaJmH nH phsH tH ChKhK5B*CJOJPJQJ\^JaJmH nH phsH tH ,hKhKOJPJQJaJmH nH sH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH ?hKhK5B*OJPJQJ\^JaJmH nH phsH tH 14MPdgmpeh|!~зззККzККz[=hKhKB*CJOJPJQJ^JaJmH nH phsH tH ?hKhK5B*OJPJQJ\^JaJmH nH phsH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH 0hKhKOJPJQJ^JaJmH nH sH tH ,hKhKOJPJQJaJmH nH sH tH 0hKhKCJOJPJQJ^JmH nH sH tH #ph(:Vv(z "ddd[$\$gdK%(:OehSVWYtwrVrrrrrrrrrrr6hKhK6CJOJPJQJ]^JmH nH sH tH 0hKhKCJOJPJQJ^JmH nH sH tH <hKhK>*B*OJPJQJ^JaJmH nH phsH tH ChKhK5B*CJOJPJQJ\^JaJmH nH phsH tH 9hKhKB*OJPJQJ^JaJmH nH phsH tH ,hKhKOJPJQJaJmH nH sH tH !47svwy!$%(8 ; w z { } """'#*#>#A#W#Z#s#v######>$E$$$雷6hKhK5OJPJQJ\^JaJmH nH sH tH 0hKhKOJPJQJ^JaJmH nH sH tH 0hKhKCJOJPJQJ^JmH nH sH tH ,hKhKOJPJQJaJmH nH sH tH ="*##$%%Y'1)*N+e-@.T...// /3/4/`/v////d7$8$H$gdK dgdKddd[$\$gdK$$%%]%`%n%q%v%y%%%%%%%%%%%P&Q&Y&Z&''','2'V'Y'_'((.)1):)?)))**鷯ззззi6hKhK5OJPJQJ\^JaJmH nH sH tH <hKhK>*B*OJPJQJ^JaJmH nH phsH tH hOh(FmH sH jh(FU0hKhKOJPJQJ^JaJmH nH sH tH 0hKhKCJOJPJQJ^JmH nH sH tH ,hKhKOJPJQJaJmH nH sH tH )*L+N+c-e->.@.R.T.................///ʱʱʖʱ|c|cccccI3hOhK>*CJOJPJQJaJmH nH sH tH 0hKhKCJOJPJQJaJmH nH sH tH 3hKhK>*CJOJPJQJaJmH nH sH tH 4hKhKCJOJPJQJ^JaJmH nH sH tH 0hKhKOJPJQJ^JaJmH nH sH tH ,hKhKOJPJQJaJmH nH sH tH <hKhK>*B*OJPJQJ^JaJmH nH phsH tH //// /2/4/5/9/:/?/`/÷nT5<hOhKB*CJOJQJ^JaJmHnHphsH tH u3hOhKCJOJQJ^JaJmHnHsH tH u<hOhKB* CJOJQJ^JaJmHnHphsH tH u<hOhKB*CJOJQJ^JaJmHnHphsH tH uhOhKmH sH hOhK5mH sH hOhKCJmH sH 0hOhKCJOJPJQJaJmH nH sH tH ,hOhKOJPJQJaJmH nH sH tH `/a/e/f/k/v/z/{//////////////////////0¨¨ᨉoZoZoZ)hKCJOJQJ^JaJmHnHtH u2hKB*CJOJQJ^JaJmHnHphtH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u///0030L0e0f0{0000001.181>1V1\111111122d7$8$H$gdK00'030@0L0Y0e0j0r0000000Ѳ~_~@~<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u3hKhKCJOJQJ^JaJmHnHsH tH u<hKhKB*CJOJQJ^JaJmHnHphsH tH u)hKCJOJQJ^JaJmHnHtH u2hKB* CJOJQJ^JaJmHnHphtH u00000161B1J1Z1d1f11ϵiO0O<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u8hKhKB*CJOJQJ^JaJmHnHphtH u2hKB* CJOJQJ^JaJmHnHphtH u)hKCJOJQJ^JaJmHnHtH u2hKB*CJOJQJ^JaJmHnHphtH u/hChKCJOJQJ^JaJmHnHtH u/hKhKCJOJQJ^JaJmHnHtH u 1111111111 2 22222!2#2$2%2)2*2,2жxYYYYY:<h%hKB*CJOJQJ^JaJmHnHphsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u<hKhKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u)hKCJOJQJ^JaJmHnHtH u3hKhKCJOJQJ^JaJmHnHsH tH u222#2E2L2m2222222E3L3M3Y3333333333d7$8$H$`gdKd7$8$H$gdK,24252:2E2F2G2J2L2P2Q2V2W2\2^2c2e2m2s2t2v2x2z2{22222222222222222222222222222222ǨǨǨǨǨǨǨǨ<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u62222222223333333"3#3(3ljjP1P1P1<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u(3031383H3J3M3T3U3W3X3a3b3g3h3j3v3w3{3ǨoP1PoǨǨ<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u{333333333333333333333333333ǨǨjPjPjPjPj3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB* CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3h%hKCJOJQJ^JaJmHnHsH tH u<h%hKB*CJOJQJ^JaJmHnHphsH tH u3333333333ǷhKh;CJ h}bhK hK52hKB* CJOJQJ^JaJmHnHphtH u2hKB*CJOJQJ^JaJmHnHphtH u 333333 dgdK21h:pK. A!S"6#8$% DyK  yK ../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htmyX;H,]ą'c simplifyDyK  yK ../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htmyX;H,]ą'c DOMarraysDyK  yK ../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htmyX;H,]ą'ctablesj 666666666vvvvvvvvv666666>6666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~ OJPJQJ_HmH nH sH tH J`J ;Normal dCJ_HaJmH sH tH f"f KTtulo 2ddd@&[$\$"5CJ$OJPJQJ\^JaJ$tH NA N Fuente de prrafo predeter.RiR 0 Tabla normal4 l4a ,k , 0 Sin lista RR K Ttulo 2 Car"5CJ$OJPJQJ\^JaJ$tH d^d K0 Normal (Web)ddd[$\$CJOJPJQJ^JaJtH <U< K0 Hipervnculo >*B*ph`g!` K0Mquina de escribir HTMLCJOJPJQJ^JaJV2V K0Texto de globo dCJOJQJ^JaJPAP K0Texto de globo CarCJOJQJ^JaJPK![Content_Types].xmlj0Eжr(΢Iw},-j4 wP-t#bΙ{UTU^hd}㨫)*1P' ^W0)T9<l#$yi};~@(Hu* Dנz/0ǰ $ X3aZ,D0j~3߶b~i>3\`?/[G\!-Rk.sԻ..a濭?PK!֧6 _rels/.relsj0 }Q%v/C/}(h"O = C?hv=Ʌ%[xp{۵_Pѣ<1H0ORBdJE4b$q_6LR7`0̞O,En7Lib/SeеPK!kytheme/theme/themeManager.xml M @}w7c(EbˮCAǠҟ7՛K Y, e.|,H,lxɴIsQ}#Ր ֵ+!,^$j=GW)E+& 8PK!&lTRtheme/theme/theme1.xmlYMoE#F{oc'vGuرHF[xw;jf7q7J\ʯ AxgfwIFPA}H1^3tH6r=2%@3'M 5BNe tYI?C K/^|Kx=#bjmo>]F,"BFVzn^3`ե̳_wr%:ϻ[k.eNVi2],S_sjcs7f W+Ն7`g ȘJj|l(KD-ʵ dXiJ؇kZov[fDNc@M!͐,a'4Y_wp >*D8i&X\,Wxҕ=6.^ۄ Z *lJ~auԙՍj9 !bM@-U8kp0vbp!971 bn w*byx$پ,@\7R(=Q,)'KQk5qpq Qz,!_ Y4[an}`B-,#U,ђMpE`35XYd״?%1U9إ;R>QD DcNU'&LGpm^9+ugVh^n<*u7ƝSdJ9gn V.rF^*Ѕ҈}-p !:P5gyڧ!# B-;Y=ۻ,K12URWV9$l{=An;sVAP9zszH'[`ۇ@PbW<{ˆ1W+m_SsncY̕([ @}`g >V?4hf6՗t#M&ʺ6'B gks:\qN-^3;k ] =Y42&0GN|tI&MI`=DCPK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 +_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!&lTRtheme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] + h 9 ~$*/`/001,22(3{333 "#%&'(*+,./012"/233!$)-3TY:?vPY+XXXX\(# AA@0(  B S  ?simplify DOMarrays"+"+ 7@FMjs"/8P'`'++ #P T   :=W]w}{TY^'`'++++33333333333333333333333333333)+++++C_<y^`.^`.pp^p`.@ @ ^@ `.^`.^`.^`.^`.PP^P`.C_<p| fKV.(F+pxhO;*++@++X+++X@UnknownG* Times New Roman5Symbol3. * Arial7.  Verdana?= * Courier New7.{ @Calibri5. *aTahomaA BCambria Math"1L|O|6%O6%O!S80++>HX  $PK2!xx   Oh+'0`   ( 4@HPX Normal 3Microsoft Office Word@@cFD@ZD6%՜.+,D՜.+,, hp|   O+  TtuloL 8@ _PID_HLINKSAAY Hhttp://developer.netscape.com/docs/manuals/communicator/jsref/stmt7.htm1004804%;../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htmtablesL~;../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htm DOMarraysBr;../../../../Escritorio/CSS-XML 2011/Javascript_ Arrays.htm simplify  !"#$%&'()*+,-./012346789:;<>?@ABCDEFGHIJKLMNPQRSTUVXYZ[\]^aRoot Entry FPW,DcData 51Table="WordDocument4hSummaryInformation(ODocumentSummaryInformation8WCompObj}  F+Documento de Microsoft Office Word 97-2003 MSWordDocWord.Document.89q