Array.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Array'>/**
  19. </span> * @class Array
  20. *
  21. * In JavaScript, the `Array` property of the global object is a constructor for
  22. * array instances.
  23. *
  24. * An array is a JavaScript object. Note that you shouldn't use it as an
  25. * associative array, use {@link Object} instead.
  26. *
  27. * # Creating an Array
  28. *
  29. * The following example creates an array, msgArray, with a length of 0, then assigns values to
  30. * msgArray[0] and msgArray[99], changing the length of the array to 100.
  31. *
  32. * var msgArray = new Array();
  33. * msgArray[0] = &quot;Hello&quot;;
  34. * msgArray[99] = &quot;world&quot;;
  35. *
  36. * if (msgArray.length == 100)
  37. * print(&quot;The length is 100.&quot;);
  38. *
  39. * # Creating a Two-dimensional Array
  40. *
  41. * The following creates chess board as a two dimensional array of strings. The first move is made by
  42. * copying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.
  43. *
  44. * var board =
  45. * [ ['R','N','B','Q','K','B','N','R'],
  46. * ['P','P','P','P','P','P','P','P'],
  47. * [' ',' ',' ',' ',' ',' ',' ',' '],
  48. * [' ',' ',' ',' ',' ',' ',' ',' '],
  49. * [' ',' ',' ',' ',' ',' ',' ',' '],
  50. * [' ',' ',' ',' ',' ',' ',' ',' '],
  51. * ['p','p','p','p','p','p','p','p'],
  52. * ['r','n','b','q','k','b','n','r']];
  53. * print(board.join('\n') + '\n\n');
  54. *
  55. * // Move King's Pawn forward 2
  56. * board[4][4] = board[6][4];
  57. * board[6][4] = ' ';
  58. * print(board.join('\n'));
  59. *
  60. * Here is the output:
  61. *
  62. * R,N,B,Q,K,B,N,R
  63. * P,P,P,P,P,P,P,P
  64. * , , , , , , ,
  65. * , , , , , , ,
  66. * , , , , , , ,
  67. * , , , , , , ,
  68. * p,p,p,p,p,p,p,p
  69. * r,n,b,q,k,b,n,r
  70. *
  71. * R,N,B,Q,K,B,N,R
  72. * P,P,P,P,P,P,P,P
  73. * , , , , , , ,
  74. * , , , , , , ,
  75. * , , , ,p, , ,
  76. * , , , , , , ,
  77. * p,p,p,p, ,p,p,p
  78. * r,n,b,q,k,b,n,r
  79. *
  80. * # Accessing array elements
  81. *
  82. * Array elements are nothing less than object properties, so they are accessed as such.
  83. *
  84. * var myArray = new Array(&quot;Wind&quot;, &quot;Rain&quot;, &quot;Fire&quot;);
  85. * myArray[0]; // &quot;Wind&quot;
  86. * myArray[1]; // &quot;Rain&quot;
  87. * // etc.
  88. * myArray.length; // 3
  89. *
  90. * // Even if indices are properties, the following notation throws a syntax error
  91. * myArray.2;
  92. *
  93. * // It should be noted that in JavaScript, object property names are strings. Consequently,
  94. * myArray[0] === myArray[&quot;0&quot;];
  95. * myArray[1] === myArray[&quot;1&quot;];
  96. * // etc.
  97. *
  98. * // However, this should be considered carefully
  99. * myArray[02]; // &quot;Fire&quot;. The number 02 is converted as the &quot;2&quot; string
  100. * myArray[&quot;02&quot;]; // undefined. There is no property named &quot;02&quot;
  101. *
  102. * # Relationship between length and numerical properties
  103. *
  104. * An array's length property and numerical properties are connected. Here is some
  105. * code explaining how this relationship works.
  106. *
  107. * var a = [];
  108. *
  109. * a[0] = 'a';
  110. * console.log(a[0]); // 'a'
  111. * console.log(a.length); // 1
  112. *
  113. * a[1] = 32;
  114. * console.log(a[1]); // 32
  115. * console.log(a.length); // 2
  116. *
  117. * a[13] = 12345;
  118. * console.log(a[13]); // 12345
  119. * console.log(a.length); // 14
  120. *
  121. * a.length = 10;
  122. * console.log(a[13]); // undefined, when reducing the length elements after length+1 are removed
  123. * console.log(a.length); // 10
  124. *
  125. * # Creating an array using the result of a match
  126. *
  127. * The result of a match between a regular expression and a string can create an array.
  128. * This array has properties and elements that provide information about the match. An
  129. * array is the return value of RegExp.exec, String.match, and String.replace. To help
  130. * explain these properties and elements, look at the following example and then refer
  131. * to the table below:
  132. *
  133. * // Match one d followed by one or more b's followed by one d
  134. * // Remember matched b's and the following d
  135. * // Ignore case
  136. *
  137. * var myRe = /d(b+)(d)/i;
  138. * var myArray = myRe.exec(&quot;cdbBdbsbz&quot;);
  139. *
  140. * The properties and elements returned from this match are as follows:
  141. *
  142. *
  143. * | Property/Element | Description | Example
  144. * |:-----------------|:--------------------------------------------------------------------------------------|:-------------------
  145. * | `input` | A read-only property that reflects the original string against which the | cdbBdbsbz
  146. * | | regular expression was matched. |
  147. * | `index` | A read-only property that is the zero-based index of the match in the string. | 1
  148. * | `[0]` | A read-only element that specifies the last matched characters. | dbBd
  149. * | `[1], ...[n]` | Read-only elements that specify the parenthesized substring matches, if included in | [1]: bB [2]: d
  150. * | | the regular expression. The number of possible parenthesized substrings is unlimited. |
  151. *
  152. * &lt;div class=&quot;notice&quot;&gt;
  153. * Documentation for this class comes from &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array&quot;&gt;MDN&lt;/a&gt;
  154. * and is available under &lt;a href=&quot;http://creativecommons.org/licenses/by-sa/2.0/&quot;&gt;Creative Commons: Attribution-Sharealike license&lt;/a&gt;.
  155. * &lt;/div&gt;
  156. *
  157. */
  158. <span id='Array-method-constructor'>/**
  159. </span> * @method constructor
  160. * Creates new Array object.
  161. *
  162. * @param {Number/Object...} items Either a number that specifies the length of array or any number of items
  163. * for the array.
  164. */
  165. // Properties
  166. <span id='Array-property-length'>/**
  167. </span> * @property {Number} length
  168. * Reflects the number of elements in an array.
  169. *
  170. * The value of the `length` property is an integer with a positive sign and a value less than 2 to the 32
  171. * power (232).
  172. *
  173. * You can set the `length` property to truncate an array at any time. When you extend an array by changing
  174. * its `length` property, the number of actual elements does not increase; for example, if you set `length`
  175. * to 3 when it is currently 2, the array still contains only 2 elements.
  176. *
  177. * In the following example the array numbers is iterated through by looking at the `length` property to see
  178. * how many elements it has. Each value is then doubled.
  179. *
  180. * var numbers = [1,2,3,4,5];
  181. * for (var i = 0; i &lt; numbers.length; i++) {
  182. * numbers[i] *= 2;
  183. * }
  184. * // numbers is now [2,4,6,8,10];
  185. *
  186. * The following example shortens the array `statesUS` to a length of 50 if the current `length` is greater
  187. * than 50.
  188. *
  189. * if (statesUS.length &gt; 50) {
  190. * statesUS.length=50
  191. * }
  192. */
  193. // Mutator methods. These methods modify the array:
  194. <span id='Array-method-pop'>/**
  195. </span> * @method pop
  196. * The pop method removes the last element from an array and returns that value to the caller.
  197. *
  198. * `pop` is intentionally generic; this method can be called or applied to objects resembling
  199. * arrays. Objects which do not contain a length property reflecting the last in a series of
  200. * consecutive, zero-based numerical properties may not behave in any meaningful manner.
  201. *
  202. * var myFish = [&quot;angel&quot;, &quot;clown&quot;, &quot;mandarin&quot;, &quot;surgeon&quot;];
  203. * var popped = myFish.pop();
  204. * alert(popped); // Alerts 'surgeon'
  205. *
  206. * @return {Object} The last element in the array
  207. */
  208. <span id='Array-method-push'>/**
  209. </span> * @method push
  210. * Adds one or more elements to the end of an array and returns the new length of the array.
  211. *
  212. * `push` is intentionally generic. This method can be called or applied to objects resembling
  213. * arrays. The push method relies on a length property to determine where to start inserting
  214. * the given values. If the length property cannot be converted into a number, the index used
  215. * is 0. This includes the possibility of length being nonexistent, in which case length will
  216. * also be created.
  217. *
  218. * The only native, array-like objects are strings, although they are not suitable in
  219. * applications of this method, as strings are immutable.
  220. *
  221. * ### Adding elements to an array
  222. *
  223. * The following code creates the sports array containing two elements, then appends two elements
  224. * to it. After the code executes, sports contains 4 elements: &quot;soccer&quot;, &quot;baseball&quot;, &quot;football&quot;
  225. * and &quot;swimming&quot;.
  226. *
  227. * var sports = [&quot;soccer&quot;, &quot;baseball&quot;];
  228. * sports.push(&quot;football&quot;, &quot;swimming&quot;);
  229. *
  230. * @param {Object...} elements The elements to add to the end of the array.
  231. * @return {Number} The new length property of the object upon which the method was called.
  232. */
  233. <span id='Array-method-reverse'>/**
  234. </span> * @method reverse
  235. * Reverses the order of the elements of an array -- the first becomes the last, and the
  236. * last becomes the first.
  237. *
  238. * The reverse method transposes the elements of the calling array object in place, mutating the
  239. * array, and returning a reference to the array.
  240. *
  241. * The following example creates an array myArray, containing three elements, then reverses the array.
  242. *
  243. * var myArray = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;];
  244. * myArray.reverse();
  245. *
  246. * This code changes myArray so that:
  247. *
  248. * - myArray[0] is &quot;three&quot;
  249. * - myArray[1] is &quot;two&quot;
  250. * - myArray[2] is &quot;one&quot;
  251. *
  252. * @return {Array} A reference to the array
  253. */
  254. <span id='Array-method-shift'>/**
  255. </span> * @method shift
  256. * Removes the first element from an array and returns that element.
  257. *
  258. * The `shift` method removes the element at the zeroeth index and shifts the values at consecutive
  259. * indexes down, then returns the removed value.
  260. *
  261. * `shift` is intentionally generic; this method can be called or applied to objects resembling
  262. * arrays. Objects which do not contain a `length` property reflecting the last in a series of
  263. * consecutive, zero-based numerical properties may not behave in any meaningful manner.
  264. *
  265. * The following code displays the `myFish` array before and after removing its first element. It also
  266. * displays the removed element:
  267. *
  268. * // assumes a println function is defined
  269. * var myFish = [&quot;angel&quot;, &quot;clown&quot;, &quot;mandarin&quot;, &quot;surgeon&quot;];
  270. * println(&quot;myFish before: &quot; + myFish);
  271. * var shifted = myFish.shift();
  272. * println(&quot;myFish after: &quot; + myFish);
  273. * println(&quot;Removed this element: &quot; + shifted);
  274. *
  275. * This example displays the following:
  276. *
  277. * myFish before: angel,clown,mandarin,surgeon
  278. * myFish after: clown,mandarin,surgeon
  279. * Removed this element: angel
  280. *
  281. * @return {Object} The first element of the array prior to shifting.
  282. */
  283. <span id='Array-method-sort'>/**
  284. </span> * @method sort
  285. * Sorts the elements of an array.
  286. *
  287. * If `compareFunction` is not supplied, elements are sorted by converting them to strings and
  288. * comparing strings in lexicographic (&quot;dictionary&quot; or &quot;telephone book,&quot; not numerical) order. For
  289. * example, &quot;80&quot; comes before &quot;9&quot; in lexicographic order, but in a numeric sort 9 comes before 80.
  290. *
  291. * If `compareFunction` is supplied, the array elements are sorted according to the return value of
  292. * the compare function. If a and b are two elements being compared, then:
  293. * If `compareFunction(a, b)` is less than 0, sort `a` to a lower index than `b`.
  294. * If `compareFunction(a, b)` returns 0, leave `a` and `b` unchanged with respect to each other, but
  295. * sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee
  296. * this behaviour, and thus not all browsers respect this.
  297. * If `compareFunction(a, b)` is greater than 0, sort `b` to a lower index than `a`.
  298. * `compareFunction(a, b)` must always returns the same value when given a specific pair of elements a
  299. * and b as its two arguments. If inconsistent results are returned then the sort order is undefined
  300. *
  301. * So, the compare function has the following form:
  302. *
  303. * function compare(a, b)
  304. * {
  305. * if (a is less than b by some ordering criterion)
  306. * return -1;
  307. * if (a is greater than b by the ordering criterion)
  308. * return 1;
  309. * // a must be equal to b
  310. * return 0;
  311. * }
  312. *
  313. * To compare numbers instead of strings, the compare function can simply subtract `b` from `a`:
  314. *
  315. * function compareNumbers(a, b)
  316. * {
  317. * return a - b;
  318. * }
  319. *
  320. * The sort() method can be conveniently used with closures:
  321. *
  322. * var numbers = [4, 2, 5, 1, 3];
  323. * numbers.sort(function(a, b) {
  324. * return a - b;
  325. * });
  326. * print(numbers);
  327. *
  328. * @param {Function} compareFunction Specifies a function that defines the sort order. If omitted, the
  329. * array is sorted lexicographically (in dictionary order) according to the string conversion of each
  330. * element.
  331. * @return {Array} A reference to the array
  332. */
  333. <span id='Array-method-splice'>/**
  334. </span> * @method splice
  335. * Adds and/or removes elements from an array.
  336. *
  337. * If you specify a different number of elements to insert than the number you're removing, the array
  338. * will have a different length at the end of the call.
  339. *
  340. * // assumes a print function is defined
  341. * var myFish = [&quot;angel&quot;, &quot;clown&quot;, &quot;mandarin&quot;, &quot;surgeon&quot;];
  342. * print(&quot;myFish: &quot; + myFish);
  343. *
  344. * var removed = myFish.splice(2, 0, &quot;drum&quot;);
  345. * print(&quot;After adding 1: &quot; + myFish);
  346. * print(&quot;removed is: &quot; + removed);
  347. *
  348. * removed = myFish.splice(3, 1);
  349. * print(&quot;After removing 1: &quot; + myFish);
  350. * print(&quot;removed is: &quot; + removed);
  351. *
  352. * removed = myFish.splice(2, 1, &quot;trumpet&quot;);
  353. * print(&quot;After replacing 1: &quot; + myFish);
  354. * print(&quot;removed is: &quot; + removed);
  355. *
  356. * removed = myFish.splice(0, 2, &quot;parrot&quot;, &quot;anemone&quot;, &quot;blue&quot;);
  357. * print(&quot;After replacing 2: &quot; + myFish);
  358. * print(&quot;removed is: &quot; + removed);
  359. *
  360. * This script displays:
  361. *
  362. * myFish: angel,clown,mandarin,surgeon
  363. * After adding 1: angel,clown,drum,mandarin,surgeon
  364. * removed is:
  365. * After removing 1: angel,clown,drum,surgeon
  366. * removed is: mandarin
  367. * After replacing 1: angel,clown,trumpet,surgeon
  368. * removed is: drum
  369. * After replacing 2: parrot,anemone,blue,trumpet,surgeon
  370. * removed is: angel,clown
  371. *
  372. * @param {Number} index Index at which to start changing the array. If negative, will begin that
  373. * many elements from the end.
  374. * @param {Number} howMany An integer indicating the number of old array elements to remove. If
  375. * `howMany` is 0, no elements are removed. In this case, you should specify at least one new element.
  376. * If no `howMany` parameter is specified all elements after index are removed.
  377. * @param {Object...} elements The elements to add to the array. If you don't specify any
  378. * elements, `splice` simply removes elements from the array.
  379. * @return {Array} An array containing the removed elements. If only one element is removed, an array
  380. * of one element is returned..
  381. */
  382. <span id='Array-method-unshift'>/**
  383. </span> * @method unshift
  384. * Adds one or more elements to the front of an array and returns the new length of the array.
  385. *
  386. * The `unshift` method inserts the given values to the beginning of an array-like object.
  387. *
  388. * `unshift` is intentionally generic; this method can be called or applied to objects resembling
  389. * arrays. Objects which do not contain a `length` property reflecting the last in a series of
  390. * consecutive, zero-based numerical properties may not behave in any meaningful manner.
  391. *
  392. * The following code displays the myFish array before and after adding elements to it.
  393. *
  394. * // assumes a println function exists
  395. * myFish = [&quot;angel&quot;, &quot;clown&quot;];
  396. * println(&quot;myFish before: &quot; + myFish);
  397. * unshifted = myFish.unshift(&quot;drum&quot;, &quot;lion&quot;);
  398. * println(&quot;myFish after: &quot; + myFish);
  399. * println(&quot;New length: &quot; + unshifted);
  400. *
  401. * This example displays the following:
  402. *
  403. * myFish before: [&quot;angel&quot;, &quot;clown&quot;]
  404. * myFish after: [&quot;drum&quot;, &quot;lion&quot;, &quot;angel&quot;, &quot;clown&quot;]
  405. * New length: 4
  406. *
  407. * @param {Object...} elements The elements to add to the front of the array.
  408. * @return {Number} The array's new length.
  409. */
  410. // Accessor methods. These methods do not modify the array and return some representation of the array.
  411. <span id='Array-method-concat'>/**
  412. </span> * @method concat
  413. * Returns a new array comprised of this array joined with other array(s) and/or value(s).
  414. *
  415. * `concat` creates a new array consisting of the elements in the `this` object on which it is called,
  416. * followed in order by, for each argument, the elements of that argument (if the argument is an
  417. * array) or the argument itself (if the argument is not an array).
  418. *
  419. * `concat` does not alter `this` or any of the arrays provided as arguments but instead returns a
  420. * &quot;one level deep&quot; copy that contains copies of the same elements combined from the original arrays.
  421. * Elements of the original arrays are copied into the new array as follows:
  422. * Object references (and not the actual object): `concat` copies object references into the new
  423. * array. Both the original and new array refer to the same object. That is, if a referenced object is
  424. * modified, the changes are visible to both the new and original arrays.
  425. * Strings and numbers (not {@link String} and {@link Number} objects): `concat` copies the values of
  426. * strings and numbers into the new array.
  427. *
  428. * Any operation on the new array will have no effect on the original arrays, and vice versa.
  429. *
  430. * ### Concatenating two arrays
  431. *
  432. * The following code concatenates two arrays:
  433. *
  434. * var alpha = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
  435. * var numeric = [1, 2, 3];
  436. *
  437. * // creates array [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, 1, 2, 3]; alpha and numeric are unchanged
  438. * var alphaNumeric = alpha.concat(numeric);
  439. *
  440. * ### Concatenating three arrays
  441. *
  442. * The following code concatenates three arrays:
  443. *
  444. * var num1 = [1, 2, 3];
  445. * var num2 = [4, 5, 6];
  446. * var num3 = [7, 8, 9];
  447. *
  448. * // creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged
  449. * var nums = num1.concat(num2, num3);
  450. *
  451. * ### Concatenating values to an array
  452. *
  453. * The following code concatenates three values to an array:
  454. *
  455. * var alpha = ['a', 'b', 'c'];
  456. *
  457. * // creates array [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, 1, 2, 3], leaving alpha unchanged
  458. * var alphaNumeric = alpha.concat(1, [2, 3]);
  459. *
  460. * @param {Object...} values Arrays and/or values to concatenate to the resulting array.
  461. * @return {Array} New array.
  462. */
  463. <span id='Array-method-join'>/**
  464. </span> * @method join
  465. * Joins all elements of an array into a string.
  466. *
  467. * The string conversions of all array elements are joined into one string.
  468. *
  469. * The following example creates an array, `a`, with three elements, then joins the array three times:
  470. * using the default separator, then a comma and a space, and then a plus.
  471. *
  472. * var a = new Array(&quot;Wind&quot;,&quot;Rain&quot;,&quot;Fire&quot;);
  473. * var myVar1 = a.join(); // assigns &quot;Wind,Rain,Fire&quot; to myVar1
  474. * var myVar2 = a.join(&quot;, &quot;); // assigns &quot;Wind, Rain, Fire&quot; to myVar2
  475. * var myVar3 = a.join(&quot; + &quot;); // assigns &quot;Wind + Rain + Fire&quot; to myVar3
  476. *
  477. * @param {String} separator Specifies a string to separate each element of the array. The separator
  478. * is converted to a string if necessary. If omitted, the array elements are separated with a comma.
  479. * @return {String} A string of the array elements.
  480. */
  481. <span id='Array-method-slice'>/**
  482. </span> * @method slice
  483. * Extracts a section of an array and returns a new array.
  484. *
  485. * `slice` does not alter the original array, but returns a new &quot;one level deep&quot; copy that contains
  486. * copies of the elements sliced from the original array. Elements of the original array are copied
  487. * into the new array as follows:
  488. * * For object references (and not the actual object), `slice` copies object references into the
  489. * new array. Both the original and new array refer to the same object. If a referenced object
  490. * changes, the changes are visible to both the new and original arrays.
  491. * * For strings and numbers (not {@link String} and {@link Number} objects), `slice` copies strings
  492. * and numbers into the new array. Changes to the string or number in one array does not affect the
  493. * other array.
  494. *
  495. * If a new element is added to either array, the other array is not affected.
  496. *
  497. * ### Using slice
  498. *
  499. * In the following example, `slice` creates a new array, `newCar`, from `myCar`. Both include a
  500. * reference to the object `myHonda`. When the color of `myHonda` is changed to purple, both arrays
  501. * reflect the change.
  502. *
  503. * // Using slice, create newCar from myCar.
  504. * var myHonda = { color: &quot;red&quot;, wheels: 4, engine: { cylinders: 4, size: 2.2 } };
  505. * var myCar = [myHonda, 2, &quot;cherry condition&quot;, &quot;purchased 1997&quot;];
  506. * var newCar = myCar.slice(0, 2);
  507. *
  508. * // Print the values of myCar, newCar, and the color of myHonda
  509. * // referenced from both arrays.
  510. * print(&quot;myCar = &quot; + myCar.toSource());
  511. * print(&quot;newCar = &quot; + newCar.toSource());
  512. * print(&quot;myCar[0].color = &quot; + myCar[0].color);
  513. * print(&quot;newCar[0].color = &quot; + newCar[0].color);
  514. *
  515. * // Change the color of myHonda.
  516. * myHonda.color = &quot;purple&quot;;
  517. * print(&quot;The new color of my Honda is &quot; + myHonda.color);
  518. *
  519. * // Print the color of myHonda referenced from both arrays.
  520. * print(&quot;myCar[0].color = &quot; + myCar[0].color);
  521. * print(&quot;newCar[0].color = &quot; + newCar[0].color);
  522. *
  523. * This script writes:
  524. *
  525. * myCar = [{color:&quot;red&quot;, wheels:4, engine:{cylinders:4, size:2.2}}, 2, &quot;cherry condition&quot;,
  526. * &quot;purchased 1997&quot;]
  527. * newCar = [{color:&quot;red&quot;, wheels:4, engine:{cylinders:4, size:2.2}}, 2]
  528. * myCar[0].color = red
  529. * newCar[0].color = red
  530. * The new color of my Honda is purple
  531. * myCar[0].color = purple
  532. * newCar[0].color = purple
  533. *
  534. * @param {Number} begin Zero-based index at which to begin extraction.
  535. * As a negative index, `start` indicates an offset from the end of the sequence. `slice(-2)` extracts
  536. * the second-to-last element and the last element in the sequence
  537. * @param {Number} end Zero-based index at which to end extraction. `slice` extracts up to but not
  538. * including `end`.
  539. * `slice(1,4)` extracts the second element through the fourth element (elements indexed 1, 2, and 3).
  540. * As a negative index, end indicates an offset from the end of the sequence. `slice(2,-1)` extracts
  541. * the third element through the second-to-last element in the sequence.
  542. * If `end` is omitted, `slice` extracts to the end of the sequence.
  543. * @return {Array} Array from the new start position up to (but not including) the specified end position.
  544. */
  545. <span id='Array-method-toString'>/**
  546. </span> * @method toString
  547. * Returns a string representing the array and its elements. Overrides the `Object.prototype.toString`
  548. * method.
  549. *
  550. * The {@link Array} object overrides the `toString` method of {@link Object}. For Array objects, the
  551. * `toString` method joins the array and returns one string containing each array element separated by
  552. * commas. For example, the following code creates an array and uses `toString` to convert the array
  553. * to a string.
  554. *
  555. * var monthNames = new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;);
  556. * myVar = monthNames.toString(); // assigns &quot;Jan,Feb,Mar,Apr&quot; to myVar
  557. *
  558. * JavaScript calls the `toString` method automatically when an array is to be represented as a text
  559. * value or when an array is referred to in a string concatenation.
  560. *
  561. * @return {String} The array as a string.
  562. */</pre>
  563. </body>
  564. </html>