PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

https://github.com/ratbeard/underscore
HTML | 1343 lines | 1201 code | 141 blank | 1 comment | 0 complexity | acff8353084b8a57f0a282e3e4ce57f5 MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="chrome=1">
  6. <title>Underscore.js</title>
  7. <style>
  8. body {
  9. font-size: 16px;
  10. line-height: 24px;
  11. background: #f0f0e5;
  12. color: #252519;
  13. font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
  14. }
  15. div.container {
  16. width: 720px;
  17. margin: 50px 0 50px 50px;
  18. }
  19. p {
  20. width: 550px;
  21. }
  22. #documentation p {
  23. margin-bottom: 4px;
  24. }
  25. a, a:visited {
  26. padding: 0 2px;
  27. text-decoration: none;
  28. background: #dadaba;
  29. color: #252519;
  30. }
  31. a:active, a:hover {
  32. color: #000;
  33. background: #f0c095;
  34. }
  35. h1, h2, h3, h4, h5, h6 {
  36. margin-top: 40px;
  37. }
  38. b.header {
  39. font-size: 18px;
  40. }
  41. span.alias {
  42. font-size: 14px;
  43. font-style: italic;
  44. margin-left: 20px;
  45. }
  46. table, tr, td {
  47. margin: 0; padding: 0;
  48. }
  49. td {
  50. padding: 2px 12px 2px 0;
  51. }
  52. code, pre, tt {
  53. font-family: Monaco, Consolas, "Lucida Console", monospace;
  54. font-size: 12px;
  55. line-height: 18px;
  56. color: #555529;
  57. }
  58. code {
  59. margin-left: 20px;
  60. }
  61. pre {
  62. font-size: 12px;
  63. padding: 2px 0 2px 12px;
  64. border-left: 6px solid #aaaa99;
  65. margin: 0px 0 30px;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <div class="container">
  71. <h1>Underscore.js</h1>
  72. <p>
  73. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  74. utility-belt library for JavaScript that provides a lot of the
  75. functional programming support that you would expect in
  76. <a href="http://prototypejs.org/api">Prototype.js</a>
  77. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  78. but without extending any of the built-in JavaScript objects. It's the
  79. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
  80. </p>
  81. <p>
  82. Underscore provides 60-odd functions that support both the usual
  83. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  84. as well as more specialized helpers: function binding, javascript
  85. templating, deep equality testing, and so on. It delegates to built-in
  86. functions, if present, so modern browsers will use the
  87. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  88. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  89. </p>
  90. <p>
  91. A complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
  92. is included for your perusal.
  93. </p>
  94. <p>
  95. The unabridged source code is
  96. <a href="http://github.com/documentcloud/underscore/">available on GitHub</a>.
  97. </p>
  98. <p>
  99. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  100. </p>
  101. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  102. <p>
  103. <table>
  104. <tr>
  105. <td><a href="underscore.js">Development Version (0.6.0)</a></td>
  106. <td><i>25kb, Uncompressed with Comments</i></td>
  107. </tr>
  108. <tr>
  109. <td><a href="underscore-min.js">Production Version (0.6.0)</a></td>
  110. <td><i>3kb, Packed and Gzipped</i></td>
  111. </tr>
  112. </table>
  113. </p>
  114. <h2>Table of Contents</h2>
  115. <a href="#styles">Object-Oriented and Functional Styles</a>
  116. <p>
  117. <b>Collections</b>
  118. <br />
  119. <span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
  120. <a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
  121. <a href="#detect">detect</a>, <a href="#select">select</a>,
  122. <a href="#reject">reject</a>, <a href="#all">all</a>,
  123. <a href="#any">any</a>, <a href="#include">include</a>,
  124. <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
  125. <a href="#max">max</a>, <a href="#min">min</a>,
  126. <a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
  127. <a href="#toArray">toArray</a>, <a href="#size">size</a></span>
  128. </p>
  129. <p>
  130. <b>Arrays</b>
  131. <br />
  132. <span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
  133. <a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
  134. <a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>,
  135. <a href="#lastIndexOf">lastIndexOf</a>, <a href="#range">range</a></span>
  136. </p>
  137. <p>
  138. <b>Functions</b>
  139. <br />
  140. <span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
  141. <a href="#defer">defer</a>, <a href="#wrap">wrap</a></span>, <a href="#compose">compose</a></span>
  142. </p>
  143. <p>
  144. <b>Objects</b>
  145. <br />
  146. <span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
  147. <a href="#functions">functions</a>, <a href="#extend">extend</a>, <a href="#clone">clone</a>, <a href="#tap">tap</a>,
  148. <a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
  149. <a href="#isArray">isArray</a>, <a href="#isArguments">isArguments</a>, <a href="#isFunction">isFunction</a>, <a href="#isString">isString</a>,
  150. <a href="#isNumber">isNumber</a>, <a href="#isBoolean">isBoolean</a>, <a href="#isDate">isDate</a>, <a href="#isRegExp">isRegExp</a>
  151. <a href="#isNaN">isNaN</a>, <a href="#isNull">isNull</a>,
  152. <a href="#isUndefined">isUndefined</a>
  153. </span>
  154. </p>
  155. <p>
  156. <b>Utility</b>
  157. <br />
  158. <span class="methods"><a href="#noConflict">noConflict</a>,
  159. <a href="#identity">identity</a>, <a href="#times">times</a>,
  160. <a href="#breakLoop">breakLoop</a></span>, <a href="#mixin">mixin</a></span>,
  161. <a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
  162. </p>
  163. <p>
  164. <b>Chaining</b>
  165. <br />
  166. <span class="methods"><a href="#chain">chain</a>, <a href="#value">value</a>
  167. </p>
  168. <div id="documentation">
  169. <h2 id="styles">Object-Oriented and Functional Styles</h2>
  170. <p>
  171. You can use Underscore in either an object-oriented or a functional style,
  172. depending on your preference. The following two lines of code are
  173. identical ways to double a list of numbers.
  174. </p>
  175. <pre>
  176. _.map([1, 2, 3], function(n){ return n * 2; });
  177. _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
  178. <p>
  179. Using the object-oriented style allows you to chain together methods. Calling
  180. <tt>chain</tt> on a wrapped object will cause all future method calls to
  181. return wrapped objects as well. When you've finished the computation,
  182. use <tt>value</tt> to retrieve the final value. Here's an example of chaining
  183. together a <b>map/flatten/reduce</b>, in order to get the word count of
  184. every word in a song.
  185. </p>
  186. <pre>
  187. var lyrics = [
  188. {line : 1, words : "I'm a lumberjack and I'm okay"},
  189. {line : 2, words : "I sleep all night and I work all day"},
  190. {line : 3, words : "He's a lumberjack and he's okay"},
  191. {line : 4, words : "He sleeps all night and he works all day"}
  192. ];
  193. _(lyrics).chain()
  194. .map(function(line) { return line.words.split(' '); })
  195. .flatten()
  196. .reduce({}, function(counts, word) {
  197. counts[word] = (counts[word] || 0) + 1;
  198. return counts;
  199. }).value();
  200. =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
  201. <p>
  202. In addition, the
  203. <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array prototype's methods</a>
  204. are proxied through the chained Underscore object, so you can slip a
  205. <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
  206. modify the array.
  207. </p>
  208. <h2>Collection Functions (Arrays or Objects)</h2>
  209. <p id="each">
  210. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  211. <span class="alias">Alias: <b>forEach</b></span>
  212. <br />
  213. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  214. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  215. passed. Each invocation of <b>iterator</b> is called with three arguments:
  216. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  217. arguments will be <tt>(value, key, list)</tt>. Use <a href="#breakLoop"><tt>breakLoop</tt></a>
  218. to break out of the iteration. Delegates to the native
  219. <b>forEach</b> function if it exists.
  220. </p>
  221. <pre>
  222. _.each([1, 2, 3], function(num){ alert(num); });
  223. =&gt; alerts each number in turn...</pre>
  224. <p id="map">
  225. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  226. <br />
  227. Produces a new array of values by mapping each value in <b>list</b>
  228. through a transformation function (<b>iterator</b>). If the native
  229. <b>map</b> method exists, it will be used instead.
  230. </p>
  231. <pre>
  232. _.map([1, 2, 3], function(num){ return num * 3 });
  233. =&gt; [3, 6, 9]</pre>
  234. <p id="reduce">
  235. <b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
  236. <span class="alias">Aliases: <b>inject, foldl</b></span>
  237. <br />
  238. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  239. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  240. of the reduction, and each successive step of it should be returned by
  241. <b>iterator</b>.
  242. </p>
  243. <pre>
  244. var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
  245. =&gt; 6
  246. </pre>
  247. <p id="reduceRight">
  248. <b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code>
  249. <span class="alias">Alias: <b>foldr</b></span>
  250. <br />
  251. The right-associative version of <b>reduce</b>. Delegates to the
  252. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  253. is not as useful in JavaScript as it would be in a language with lazy
  254. evaluation.
  255. </p>
  256. <pre>
  257. var list = [[0, 1], [2, 3], [4, 5]];
  258. var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); });
  259. =&gt; [4, 5, 2, 3, 0, 1]
  260. </pre>
  261. <p id="detect">
  262. <b class="header">detect</b><code>_.detect(list, iterator, [context])</code>
  263. <br />
  264. Looks through each value in the <b>list</b>, returning the first one that
  265. passes a truth test (<b>iterator</b>). The function returns as
  266. soon as it finds an acceptable element, and doesn't traverse the
  267. entire list.
  268. </p>
  269. <pre>
  270. var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  271. =&gt; 2
  272. </pre>
  273. <p id="select">
  274. <b class="header">select</b><code>_.select(list, iterator, [context])</code>
  275. <span class="alias">Alias: <b>filter</b></span>
  276. <br />
  277. Looks through each value in the <b>list</b>, returning an array of all
  278. the values that pass a truth test (<b>iterator</b>). Delegates to the
  279. native <b>filter</b> method, if it exists.
  280. </p>
  281. <pre>
  282. var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  283. =&gt; [2, 4, 6]
  284. </pre>
  285. <p id="reject">
  286. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  287. <br />
  288. Returns the values in <b>list</b> without the elements that the truth
  289. test (<b>iterator</b>) passes. The opposite of <b>select</b>.
  290. </p>
  291. <pre>
  292. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  293. =&gt; [1, 3, 5]
  294. </pre>
  295. <p id="all">
  296. <b class="header">all</b><code>_.all(list, [iterator], [context])</code>
  297. <span class="alias">Alias: <b>every</b></span>
  298. <br />
  299. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  300. truth test. If an <b>iterator</b> is not provided, the truthy value of
  301. the element will be used instead. Delegates to the native method <b>every</b>, if
  302. present.
  303. </p>
  304. <pre>
  305. _.all([true, 1, null, 'yes']);
  306. =&gt; false
  307. </pre>
  308. <p id="any">
  309. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  310. <span class="alias">Alias: <b>some</b></span>
  311. <br />
  312. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  313. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  314. if a true element is found. Delegates to the native method <b>some</b>,
  315. if present.
  316. </p>
  317. <pre>
  318. _.any([null, 0, 'yes', false]);
  319. =&gt; true
  320. </pre>
  321. <p id="include">
  322. <b class="header">include</b><code>_.include(list, value)</code>
  323. <br />
  324. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
  325. <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
  326. is an Array.
  327. </p>
  328. <pre>
  329. _.include([1, 2, 3], 3);
  330. =&gt; true
  331. </pre>
  332. <p id="invoke">
  333. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  334. <br />
  335. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  336. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  337. method invocation.
  338. </p>
  339. <pre>
  340. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  341. =&gt; [[1, 5, 7], [1, 2, 3]]
  342. </pre>
  343. <p id="pluck">
  344. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  345. <br />
  346. An convenient version of what is perhaps the most common use-case for
  347. <b>map</b>: extracting a list of property values.
  348. </p>
  349. <pre>
  350. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  351. _.pluck(stooges, 'name');
  352. =&gt; ["moe", "larry", "curly"]
  353. </pre>
  354. <p id="max">
  355. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  356. <br />
  357. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  358. it will be used on each value to generate the criterion by which the
  359. value is ranked.
  360. </p>
  361. <pre>
  362. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  363. _.max(stooges, function(stooge){ return stooge.age; });
  364. =&gt; {name : 'curly', age : 60};
  365. </pre>
  366. <p id="min">
  367. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  368. <br />
  369. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  370. it will be used on each value to generate the criterion by which the
  371. value is ranked.
  372. </p>
  373. <pre>
  374. var numbers = [10, 5, 100, 2, 1000];
  375. _.min(numbers);
  376. =&gt; 2
  377. </pre>
  378. <p id="sortBy">
  379. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  380. <br />
  381. Returns a sorted <b>list</b>, ranked by the results of running each
  382. value through <b>iterator</b>.
  383. </p>
  384. <pre>
  385. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  386. =&gt; [5, 4, 6, 3, 1, 2]
  387. </pre>
  388. <p id="sortedIndex">
  389. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  390. <br />
  391. Uses a binary search to determine the index at which the <b>value</b>
  392. should be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  393. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  394. the sort ranking of each value.
  395. </p>
  396. <pre>
  397. _.sortedIndex([10, 20, 30, 40, 50], 35);
  398. =&gt; 3
  399. </pre>
  400. <p id="toArray">
  401. <b class="header">toArray</b><code>_.toArray(list)</code>
  402. <br />
  403. Converts the <b>list</b> (anything that can be iterated over), into a
  404. real Array. Useful for transmuting the <b>arguments</b> object.
  405. </p>
  406. <pre>
  407. (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
  408. =&gt; [1, 2, 3]
  409. </pre>
  410. <p id="size">
  411. <b class="header">size</b><code>_.size(list)</code>
  412. <br />
  413. Return the number of values in the <b>list</b>.
  414. </p>
  415. <pre>
  416. _.size({one : 1, two : 2, three : 3});
  417. =&gt; 3
  418. </pre>
  419. <h2>Array Functions</h2>
  420. <p>
  421. <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
  422. </p>
  423. <p id="first">
  424. <b class="header">first</b><code>_.first(array, [n])</code>
  425. <span class="alias">Alias: <b>head</b></span>
  426. <br />
  427. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  428. return the first <b>n</b> elements of the array.
  429. </p>
  430. <pre>
  431. _.first([5, 4, 3, 2, 1]);
  432. =&gt; 5
  433. </pre>
  434. <p id="rest">
  435. <b class="header">rest</b><code>_.rest(array, [index])</code>
  436. <span class="alias">Alias: <b>tail</b></span>
  437. <br />
  438. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  439. to return the values of the array from that index onward.
  440. </p>
  441. <pre>
  442. _.rest([5, 4, 3, 2, 1]);
  443. =&gt; [4, 3, 2, 1]
  444. </pre>
  445. <p id="last">
  446. <b class="header">last</b><code>_.last(array)</code>
  447. <br />
  448. Returns the last element of an <b>array</b>.
  449. </p>
  450. <pre>
  451. _.last([5, 4, 3, 2, 1]);
  452. =&gt; 1
  453. </pre>
  454. <p id="compact">
  455. <b class="header">compact</b><code>_.compact(array)</code>
  456. <br />
  457. Returns a copy of the <b>array</b> with all falsy values removed.
  458. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  459. <i>undefined</i> and <i>NaN</i> are all falsy.
  460. </p>
  461. <pre>
  462. _.compact([0, 1, false, 2, '', 3]);
  463. =&gt; [1, 2, 3]
  464. </pre>
  465. <p id="flatten">
  466. <b class="header">flatten</b><code>_.flatten(array)</code>
  467. <br />
  468. Flattens a nested <b>array</b> (the nesting can be to any depth).
  469. </p>
  470. <pre>
  471. _.flatten([1, [2], [3, [[[4]]]]]);
  472. =&gt; [1, 2, 3, 4];
  473. </pre>
  474. <p id="without">
  475. <b class="header">without</b><code>_.without(array, [*values])</code>
  476. <br />
  477. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  478. removed. <i>===</i> is used for the equality test.
  479. </p>
  480. <pre>
  481. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  482. =&gt; [2, 3, 4]
  483. </pre>
  484. <p id="uniq">
  485. <b class="header">uniq</b><code>_.uniq(array, [isSorted])</code>
  486. <br />
  487. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  488. object equality. If you know in advance that the <b>array</b> is sorted,
  489. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  490. </p>
  491. <pre>
  492. _.uniq([1, 2, 1, 3, 1, 4]);
  493. =&gt; [1, 2, 3, 4]
  494. </pre>
  495. <p id="intersect">
  496. <b class="header">intersect</b><code>_.intersect(*arrays)</code>
  497. <br />
  498. Computes the list of values that are the intersection of all the <b>arrays</b>.
  499. Each value in the result is present in each of the <b>arrays</b>.
  500. </p>
  501. <pre>
  502. _.intersect([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  503. =&gt; [1, 2]
  504. </pre>
  505. <p id="zip">
  506. <b class="header">zip</b><code>_.zip(*arrays)</code>
  507. <br />
  508. Merges together the values of each of the <b>arrays</b> with the
  509. values at the corresponding position. Useful when you have separate
  510. data sources that are coordinated through matching array indexes.
  511. </p>
  512. <pre>
  513. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  514. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  515. </pre>
  516. <p id="indexOf">
  517. <b class="header">indexOf</b><code>_.indexOf(array, value)</code>
  518. <br />
  519. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  520. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  521. <b>indexOf</b> function unless it's missing.
  522. </p>
  523. <pre>
  524. _.indexOf([1, 2, 3], 2);
  525. =&gt; 1
  526. </pre>
  527. <p id="lastIndexOf">
  528. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
  529. <br />
  530. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  531. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  532. function if possible.
  533. </p>
  534. <pre>
  535. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  536. =&gt; 4
  537. </pre>
  538. <p id="range">
  539. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  540. <br />
  541. A function to create flexibly-numbered lists of integers, handy for
  542. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  543. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  544. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  545. exclusive.
  546. </p>
  547. <pre>
  548. _.range(10);
  549. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  550. _.range(1, 11);
  551. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  552. _.range(0, 30, 5);
  553. =&gt; [0, 5, 10, 15, 20, 25]
  554. _.range(0, -10, -1);
  555. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  556. _.range(0);
  557. =&gt; []
  558. </pre>
  559. <h2>Function (uh, ahem) Functions</h2>
  560. <p id="bind">
  561. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  562. <br />
  563. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  564. the function is called, the value of <i>this</i> will be the <b>object</b>.
  565. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  566. also known as <b>currying</b>.
  567. </p>
  568. <pre>
  569. var func = function(greeting){ return greeting + ': ' + this.name };
  570. func = _.bind(func, {name : 'moe'}, 'hi');
  571. func();
  572. =&gt; 'hi: moe'
  573. </pre>
  574. <p id="bindAll">
  575. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  576. <br />
  577. Binds a number of methods on the <b>object</b>, specified by
  578. <b>methodNames</b>, to be run in the context of that object whenever they
  579. are invoked. Very handy for binding functions that are going to be used
  580. as event handlers, which would otherwise be invoked with a fairly useless
  581. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  582. function properties will be bound to it.
  583. </p>
  584. <pre>
  585. var buttonView = {
  586. label : 'underscore',
  587. onClick : function(){ alert('clicked: ' + this.label); },
  588. onHover : function(){ console.log('hovering: ' + this.label); }
  589. };
  590. _.bindAll(buttonView);
  591. jQuery('#underscore_button').bind('click', buttonView.onClick);
  592. =&gt; When the button is clicked, this.label will have the correct value...
  593. </pre>
  594. <p id="delay">
  595. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  596. <br />
  597. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  598. milliseconds. If you pass the optional <b>arguments</b>, they will be
  599. forwarded on to the <b>function</b> when it is invoked.
  600. </p>
  601. <pre>
  602. var log = _.bind(console.log, console);
  603. _.delay(log, 1000, 'logged later');
  604. =&gt; 'logged later' // Appears after one second.
  605. </pre>
  606. <p id="defer">
  607. <b class="header">defer</b><code>_.defer(function)</code>
  608. <br />
  609. Defers invoking the <b>function</b> until the current call stack has cleared,
  610. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  611. expensive computations or HTML rendering in chunks without blocking the UI thread
  612. from updating.
  613. </p>
  614. <pre>
  615. _.defer(function(){ alert('deferred'); });
  616. // Returns from the function before the alert runs.
  617. </pre>
  618. <p id="wrap">
  619. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  620. <br />
  621. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  622. passing it as the first argument. This allows the <b>wrapper</b> to
  623. execute code before and after the <b>function</b> runs, adjust the arguments,
  624. and execute it conditionally.
  625. </p>
  626. <pre>
  627. var hello = function(name) { return "hello: " + name; };
  628. hello = _.wrap(hello, function(func) {
  629. return "before, " + func("moe") + ", after";
  630. });
  631. hello();
  632. =&gt; 'before, hello: moe, after'
  633. </pre>
  634. <p id="compose">
  635. <b class="header">compose</b><code>_.compose(*functions)</code>
  636. <br />
  637. Returns the composition of a list of <b>functions</b>, where each function
  638. consumes the return value of the function that follows. In math terms,
  639. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  640. <i>f(g(h()))</i>.
  641. </p>
  642. <pre>
  643. var greet = function(name){ return "hi: " + name; };
  644. var exclaim = function(statement){ return statement + "!"; };
  645. var welcome = _.compose(greet, exclaim);
  646. welcome('moe');
  647. =&gt; 'hi: moe!'
  648. </pre>
  649. <h2>Object Functions</h2>
  650. <p id="keys">
  651. <b class="header">keys</b><code>_.keys(object)</code>
  652. <br />
  653. Retrieve all the names of the <b>object</b>'s properties.
  654. </p>
  655. <pre>
  656. _.keys({one : 1, two : 2, three : 3});
  657. =&gt; ["one", "two", "three"]
  658. </pre>
  659. <p id="values">
  660. <b class="header">values</b><code>_.values(object)</code>
  661. <br />
  662. Return all of the values of the <b>object</b>'s properties.
  663. </p>
  664. <pre>
  665. _.values({one : 1, two : 2, three : 3});
  666. =&gt; [1, 2, 3]
  667. </pre>
  668. <p id="functions">
  669. <b class="header">functions</b><code>_.functions(object)</code>
  670. <span class="alias">Alias: <b>methods</b></span>
  671. <br />
  672. Returns a sorted list of the names of every method in an object &mdash;
  673. that is to say, the name of every function property of the object.
  674. </p>
  675. <pre>
  676. _.functions(_);
  677. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  678. </pre>
  679. <p id="extend">
  680. <b class="header">extend</b><code>_.extend(destination, source)</code>
  681. <br />
  682. Copy all of the properties in the <b>source</b> object over to the
  683. <b>destination</b> object.
  684. </p>
  685. <pre>
  686. _.extend({name : 'moe'}, {age : 50});
  687. =&gt; {name : 'moe', age : 50}
  688. </pre>
  689. <p id="clone">
  690. <b class="header">clone</b><code>_.clone(object)</code>
  691. <br />
  692. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  693. or arrays will be copied by reference, not duplicated.
  694. </p>
  695. <pre>
  696. _.clone({name : 'moe'});
  697. =&gt; {name : 'moe'};
  698. </pre>
  699. <p id="tap">
  700. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  701. <br />
  702. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  703. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  704. </p>
  705. <pre>
  706. _([1,2,3,200]).chain().
  707. select(function(num) { return num % 2 == 0; }).
  708. tap(console.log).
  709. map(function(num) { return num * num }).
  710. value();
  711. =&gt; [2, 200]
  712. =&gt; [4, 40000]
  713. </pre>
  714. <p id="isEqual">
  715. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  716. <br />
  717. Performs an optimized deep comparison between the two objects, to determine
  718. if they should be considered equal.
  719. </p>
  720. <pre>
  721. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  722. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  723. moe == clone;
  724. =&gt; false
  725. _.isEqual(moe, clone);
  726. =&gt; true
  727. </pre>
  728. <p id="isEmpty">
  729. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  730. <br />
  731. Returns <i>true</i> if <b>object</b> contains no values.
  732. </p>
  733. <pre>
  734. _.isEmpty([1, 2, 3]);
  735. =&gt; false
  736. _.isEmpty({});
  737. =&gt; true
  738. </pre>
  739. <p id="isElement">
  740. <b class="header">isElement</b><code>_.isElement(object)</code>
  741. <br />
  742. Returns <i>true</i> if <b>object</b> is a DOM element.
  743. </p>
  744. <pre>
  745. _.isElement(jQuery('body')[0]);
  746. =&gt; true
  747. </pre>
  748. <p id="isArray">
  749. <b class="header">isArray</b><code>_.isArray(object)</code>
  750. <br />
  751. Returns <i>true</i> if <b>object</b> is an Array.
  752. </p>
  753. <pre>
  754. (function(){ return _.isArray(arguments); })();
  755. =&gt; false
  756. _.isArray([1,2,3]);
  757. =&gt; true
  758. </pre>
  759. <p id="isArguments">
  760. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  761. <br />
  762. Returns <i>true</i> if <b>object</b> is an Arguments object.
  763. </p>
  764. <pre>
  765. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  766. =&gt; true
  767. _.isArguments([1,2,3]);
  768. =&gt; false
  769. </pre>
  770. <p id="isFunction">
  771. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  772. <br />
  773. Returns <i>true</i> if <b>object</b> is a Function.
  774. </p>
  775. <pre>
  776. _.isFunction(alert);
  777. =&gt; true
  778. </pre>
  779. <p id="isString">
  780. <b class="header">isString</b><code>_.isString(object)</code>
  781. <br />
  782. Returns <i>true</i> if <b>object</b> is a String.
  783. </p>
  784. <pre>
  785. _.isFunction("moe");
  786. =&gt; true
  787. </pre>
  788. <p id="isNumber">
  789. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  790. <br />
  791. Returns <i>true</i> if <b>object</b> is a Number.
  792. </p>
  793. <pre>
  794. _.isNumber(8.4 * 5);
  795. =&gt; true
  796. </pre>
  797. <p id="isBoolean">
  798. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  799. <br />
  800. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  801. </p>
  802. <pre>
  803. _.isBoolean(null);
  804. =&gt; false
  805. </pre>
  806. <p id="isDate">
  807. <b class="header">isDate</b><code>_.isDate(object)</code>
  808. <br />
  809. Returns <i>true</i> if <b>object</b> is a Date.
  810. </p>
  811. <pre>
  812. _.isDate(new Date());
  813. =&gt; true
  814. </pre>
  815. <p id="isRegExp">
  816. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  817. <br />
  818. Returns <i>true</i> if <b>object</b> is a RegExp.
  819. </p>
  820. <pre>
  821. _.isRegExp(/moe/);
  822. =&gt; true
  823. </pre>
  824. <p id="isNaN">
  825. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  826. <br />
  827. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  828. the same as the native <b>isNaN</b> function, which will also return
  829. true if the variable is <i>undefined</i>.
  830. </p>
  831. <pre>
  832. _.isNaN(NaN);
  833. =&gt; true
  834. isNaN(undefined);
  835. =&gt; true
  836. _.isNaN(undefined);
  837. =&gt; false
  838. </pre>
  839. <p id="isNull">
  840. <b class="header">isNull</b><code>_.isNull(object)</code>
  841. <br />
  842. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  843. </p>
  844. <pre>
  845. _.isNull(null);
  846. =&gt; true
  847. _.isNull(undefined);
  848. =&gt; false
  849. </pre>
  850. <p id="isUndefined">
  851. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  852. <br />
  853. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  854. </p>
  855. <pre>
  856. _.isUndefined(window.missingVariable);
  857. =&gt; true
  858. </pre>
  859. <h2>Utility Functions</h2>
  860. <p id="noConflict">
  861. <b class="header">noConflict</b><code>_.noConflict()</code>
  862. <br />
  863. Give control of the "_" variable back to its previous owner. Returns
  864. a reference to the <b>Underscore</b> object.
  865. </p>
  866. <pre>
  867. var underscore = _.noConflict();</pre>
  868. <p id="identity">
  869. <b class="header">identity</b><code>_.identity(value)</code>
  870. <br />
  871. Returns the same value that is used as the argument. In math:
  872. <tt>f(x) = x</tt><br />
  873. This function looks useless, but is used throughout Underscore as
  874. a default iterator.
  875. </p>
  876. <pre>
  877. var moe = {name : 'moe'};
  878. moe === _.identity(moe);
  879. =&gt; true</pre>
  880. <p id="times">
  881. <b class="header">times</b><code>_.times(n, iterator)</code>
  882. <br />
  883. Invokes the given iterator function <b>n</b> times.
  884. </p>
  885. <pre>
  886. _(3).times(function(){ genie.grantWish(); });</pre>
  887. <p id="breakLoop">
  888. <b class="header">breakLoop</b><code>_.breakLoop()</code>
  889. <br />
  890. Breaks out of the current loop iteration. Similar to the <tt>break</tt>
  891. keyword in regular "for" loop, but works within an iterator function.
  892. Uses the native <tt>StopIteration</tt> object in JavaScript 1.7 compliant
  893. browsers.
  894. </p>
  895. <pre>
  896. var result = null;
  897. _.each([1, 2, 3], function(num) {
  898. if ((result = num) == 2) _.breakLoop();
  899. });
  900. result;
  901. =&gt; 2</pre>
  902. <p id="mixin">
  903. <b class="header">mixin</b><code>_.mixin(object)</code>
  904. <br />
  905. Allows you to extend Underscore with your own utility functions. Pass
  906. a hash of <tt>{name: function}</tt> definitions to have your functions
  907. added to the Underscore object, as well as the OOP wrapper.
  908. </p>
  909. <pre>
  910. _.mixin({
  911. capitalize : function(string) {
  912. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  913. }
  914. });
  915. _("fabio").capitalize();
  916. =&gt; "Fabio"
  917. </pre>
  918. <p id="uniqueId">
  919. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  920. <br />
  921. Generate a globally-unique id for client-side models or DOM elements
  922. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  923. </p>
  924. <pre>
  925. _.uniqueId('contact_');
  926. =&gt; 'contact_104'</pre>
  927. <p id="template">
  928. <b class="header">template</b><code>_.template(templateString, [context])</code>
  929. <br />
  930. Compiles JavaScript templates into functions that can be evaluated
  931. for rendering. Useful for rendering complicated bits of HTML from JSON
  932. data sources. Template functions can both interpolate variables, using<br />
  933. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  934. <tt>&lt;% &hellip; %&gt;</tt>. When you evaluate a template function, pass in a
  935. <b>context</b> object that has properties corresponding to the template's free
  936. variables. If you're writing a one-off, you can pass the <b>context</b>
  937. object as the second parameter to <b>template</b> in order to render
  938. immediately instead of returning a template function.
  939. </p>
  940. <pre>
  941. var compiled = _.template("hello: &lt;%= name %&gt;");
  942. compiled({name : 'moe'});
  943. =&gt; "hello: moe"
  944. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  945. _.template(list, {people : ['moe', 'curly', 'larry']});
  946. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"</pre>
  947. <p>
  948. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  949. template settings to use different symbols to set off interpolated code.
  950. Define <b>start</b> and <b>end</b> tokens, and an <b>interpolate</b> regex
  951. to match expressions that should be evaluated and inserted. For example,
  952. to perform
  953. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  954. style templating:
  955. </p>
  956. <pre>
  957. _.templateSettings = {
  958. start : '{{',
  959. end : '}}',
  960. interpolate : /\{\{(.+?)\}\}/g
  961. };
  962. var template = _.template("Hello {{ name }}!");
  963. template({name : "Mustache"});
  964. =&gt; "Hello Mustache!"</pre>
  965. <h2>Chaining</h2>
  966. <p id="chain">
  967. <b class="header">chain</b><code>_(obj).chain()</code>
  968. <br />
  969. Returns a wrapped object. Calling methods on this object will continue
  970. to return wrapped objects until <tt>value</tt> is used. (
  971. <a href="#styles">A more realistic example.</a>)
  972. </p>
  973. <pre>
  974. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  975. var youngest = _(stooges).chain()
  976. .sortBy(function(stooge){ return stooge.age; })
  977. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  978. .first()
  979. .value();
  980. =&gt; "moe is 21"
  981. </pre>
  982. <p id="value">
  983. <b class="header">value</b><code>_(obj).value()</code>
  984. <br />
  985. Extracts the value of a wrapped object.
  986. </p>
  987. <pre>
  988. _([1, 2, 3]).value();
  989. =&gt; [1, 2, 3]
  990. </pre>
  991. <h2 id="duck_typing">Duck Typing</h2>
  992. <p>
  993. The <b>isType</b> (<tt>isArray</tt>, <tt>isFunction</tt>, <tt>isString</tt> ...) family of type-checking
  994. functions use property detection to do their work, which, although
  995. orders of magnitude faster than the alternative, isn't entirely safe when dealing
  996. with objects that are used as hashes, where arbitrary strings are being
  997. set for the keys. It's entirely possible for an object to masquerade as
  998. another type, if you're setting properties with names like "concat" and
  999. "charCodeAt". So be aware.
  1000. </p>
  1001. <h2>Links &amp; Suggested Reading</h2>
  1002. <p>
  1003. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  1004. a Lua port of the functions that are applicable in both languages.
  1005. Includes OOP-wrapping and chaining.
  1006. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1007. available on GitHub.
  1008. </p>
  1009. <p>
  1010. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1011. </p>
  1012. <p>
  1013. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1014. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1015. </p>
  1016. <p>
  1017. Oliver Steele's
  1018. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1019. which includes comprehensive higher-order function support as well as string lambdas.
  1020. </p>
  1021. <p>
  1022. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1023. </p>
  1024. <h2>Change Log</h2>
  1025. <p>
  1026. <b class="header">0.6.0</b><br />
  1027. Major release. Incorporates a number of
  1028. <a href="http://github.com/ratbeard">Mile Frawley's</a> refactors for
  1029. safer duck-typing on collection functions, and cleaner internals. A new
  1030. <tt>_.mixin</tt> method that allows you to extend Underscore with utility
  1031. functions of your own. Added <tt>_.times</tt>, which works the same as in
  1032. Ruby or Prototype.js. Native support for ECMAScript 5's <tt>Array.isArray</tt>,
  1033. and <tt>Object.keys</tt>.
  1034. </p>
  1035. <p>
  1036. <b class="header">0.5.8</b><br />
  1037. Fixed Underscore's collection functions to work on
  1038. <a href="https://developer.mozilla.org/En/DOM/NodeList">NodeLists</a> and
  1039. <a href="https://developer.mozilla.org/En/DOM/HTMLCollection">HTMLCollections</a>
  1040. once more, thanks to
  1041. <a href="http://github.com/jmtulloss">Justin Tulloss</a>.
  1042. </p>
  1043. <p>
  1044. <b class="header">0.5.7</b><br />
  1045. A safer implementation of <tt>_.isArguments</tt>, and a
  1046. faster <tt>_.isNumber</tt>,<br />thanks to
  1047. <a href="http://jedschmidt.com/">Jed Schmidt</a>.
  1048. </p>
  1049. <p>
  1050. <b class="header">0.5.6</b><br />
  1051. Customizable delimiters for <tt>_.template</tt>, contributed by
  1052. <a href="http://github.com/iamnoah">Noah Sloan</a>.
  1053. </p>
  1054. <p>
  1055. <b class="header">0.5.5</b><br />
  1056. Fix for a bug in MobileSafari's OOP-wrapper, with the arguments object.
  1057. </p>
  1058. <p>
  1059. <b class="header">0.5.4</b><br />
  1060. Fix for multiple single quotes within a template string for
  1061. <tt>_.template</tt>. See:
  1062. <a href="http://www.west-wind.com/Weblog/posts/509108.aspx">Rick Strahl's blog post</a>.
  1063. </p>
  1064. <p>
  1065. <b class="header">0.5.2</b><br />
  1066. New implementations of <tt>isArray</tt>, <tt>isDate</tt>, <tt>isFunction</tt>,
  1067. <tt>isNumber</tt>, <tt>isRegExp</tt>, and <tt>isString</tt>, thanks to
  1068. a suggestion from
  1069. <a href="http://www.broofa.com/">Robert Kieffer</a>.
  1070. Instead of doing <tt>Object#toString</tt>
  1071. comparisons, they now check for expected properties, which is less safe,
  1072. but more than an order of magnitude faster. Most other Underscore
  1073. functions saw minor speed improvements as a result.
  1074. <a href="http://dolzhenko.org/">Evgeniy Dolzhenko</a>
  1075. contributed <tt>_.tap</tt>,
  1076. <a href="http://ruby-doc.org/core-1.9/classes/Object.html#M000191">similar to Ruby 1.9's</a>,
  1077. which is handy for injecting side effects (like logging) into chained calls.
  1078. </p>
  1079. <p>
  1080. <b class="header">0.5.1</b><br />
  1081. Added an <tt>_.isArguments</tt> function. Lots of little safety checks
  1082. and optimizations contributed by
  1083. <a href="http://github.com/iamnoah/">Noah Sloan</a> and Andri Möll.
  1084. </p>
  1085. <p>
  1086. <b class="header">0.5.0</b><br />
  1087. <b>[API Changes]</b> <tt>_.bindAll</tt> now takes the context object as
  1088. its first parameter. If no method names are passed, all of the context
  1089. object's methods are bound to it, enabling chaining and easier binding.
  1090. <tt>_.functions</tt> now takes a single argument and returns the names
  1091. of its Function properties. Calling <tt>_.functions(_)</tt> will get you
  1092. the previous behavior.
  1093. Added <tt>_.isRegExp</tt> so that <tt>isEqual</tt> can now test for RegExp equality.
  1094. All of the "is" functions have been shrunk down into a single definition.
  1095. <a href="http://github.com/grayrest/">Karl Guertin</a> contributed patches.
  1096. </p>
  1097. <p>
  1098. <b class="header">0.4.7</b><br />
  1099. Added <tt>isDate</tt>, <tt>isNaN</tt>, and <tt>isNull</tt>, for completeness.
  1100. Optimizations for <tt>isEqual</tt> when checking equality between Arrays
  1101. or Dates. <tt>_.keys</tt> is now <small><i><b>25%&ndash;2X</b></i></small> faster (depending on your
  1102. browser) which speeds up the functions that rely on it, such as <tt>_.each</tt>.
  1103. </p>
  1104. <p>
  1105. <b class="header">0.4.6</b><br />
  1106. Added the <tt>range</tt> function, a port of the
  1107. <a href="http://docs.python.org/library/functions.html#range">Python
  1108. function of the same name</a>, for generating flexibly-numbered lists
  1109. of integers. Original patch contributed by
  1110. <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
  1111. </p>
  1112. <p>
  1113. <b class="header">0.4.5</b><br />
  1114. Added <tt>rest</tt> for Arrays and arguments objects, and aliased
  1115. <tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
  1116. thanks to <a href="http://github.com/lukesutton/">Luke Sutton</a>'s patches.
  1117. Added tests ensuring that all Underscore Array functions also work on
  1118. <i>arguments</i> objects.
  1119. </p>
  1120. <p>
  1121. <b class="header">0.4.4</b><br />
  1122. Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
  1123. <tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
  1124. </p>
  1125. <p>
  1126. <b class="header">0.4.3</b><br />
  1127. Started using the native <tt>StopIteration</tt> object in browsers that support it.
  1128. Fixed Underscore setup for CommonJS environments.
  1129. </p>
  1130. <p>
  1131. <b class="header">0.4.2</b><br />
  1132. Renamed the unwrapping function to <tt>value</tt>, for clarity.
  1133. </p>
  1134. <p>
  1135. <b class="header">0.4.1</b><br />
  1136. Chained Underscore objects now support the Array prototype methods, so
  1137. that you can perform the full range of operations on a wrapped array
  1138. without having to break your chain. Added a <tt>breakLoop</tt> method
  1139. to <b>break</b> in the middle of any Underscore iteration. Added an
  1140. <tt>isEmpty</tt> function that works on arrays and objects.
  1141. </p>
  1142. <p>
  1143. <b class="header">0.4.0</b><br />
  1144. All Underscore functions can now be called in an object-oriented style,
  1145. like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
  1146. <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
  1147. Wrapped objects can be chained through multiple
  1148. method invocations. A <a href="#functions"><tt>functions</tt></a> method
  1149. was added, providing a sorted list of all the functions in Underscore.
  1150. </p>
  1151. <p>
  1152. <b class="header">0.3.3</b><br />
  1153. Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
  1154. as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
  1155. </p>
  1156. <p>
  1157. <b class="header">0.3.2</b><br />
  1158. Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
  1159. interpreters with: <tt>load("underscore.js")</tt>.
  1160. Added <a href="#identity"><tt>identity</tt></a> as a utility function.
  1161. </p>
  1162. <p>
  1163. <b class="header">0.3.1</b><br />
  1164. All iterators are now passed in the original collection as their third
  1165. argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
  1166. objects is now called with <tt>(value, key, collection)</tt>, for details
  1167. see <a href="#each"><tt>_.each</tt></a>.
  1168. </p>
  1169. <p>
  1170. <b class="header">0.3.0</b><br />
  1171. Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
  1172. comprehensive optimizations, merged in
  1173. <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
  1174. <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
  1175. <a href="http://narwhaljs.org/">Narwhal</a> compliant.
  1176. </p>
  1177. <p>
  1178. <b class="header">0.2.0</b><br />
  1179. Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
  1180. <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
  1181. <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
  1182. </p>
  1183. <p>
  1184. <b class="header">0.1.1</b><br />
  1185. Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
  1186. other variables.
  1187. </p>
  1188. <p>
  1189. <b class="header">0.1.0</b><br />
  1190. Initial release of Underscore.js.
  1191. </p>
  1192. <p>
  1193. <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
  1194. <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
  1195. </a>
  1196. </p>
  1197. </div>
  1198. </div>
  1199. <!-- Include Underscore, so you can play with it in the console. -->
  1200. <script type="text/javascript" src="underscore.js"></script>
  1201. </body>
  1202. </html>