PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

https://github.com/weplay/underscore
HTML | 1364 lines | 1219 code | 144 blank | 1 comment | 0 complexity | 085503fcdfaa5dd69aedd93dc609077d 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 (1.0.2)</a></td>
  106. <td><i>25kb, Uncompressed with Comments</i></td>
  107. </tr>
  108. <tr>
  109. <td><a href="underscore-min.js">Production Version (1.0.2)</a></td>
  110. <td><i>2.5kb, 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, *sources)</code>
  681. <br />
  682. Copy all of the properties in the <b>source</b> objects over to the
  683. <b>destination</b> object. It's in-order, to the last source will override
  684. properties of the same name in previous arguments.
  685. </p>
  686. <pre>
  687. _.extend({name : 'moe'}, {age : 50});
  688. =&gt; {name : 'moe', age : 50}
  689. </pre>
  690. <p id="clone">
  691. <b class="header">clone</b><code>_.clone(object)</code>
  692. <br />
  693. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  694. or arrays will be copied by reference, not duplicated.
  695. </p>
  696. <pre>
  697. _.clone({name : 'moe'});
  698. =&gt; {name : 'moe'};
  699. </pre>
  700. <p id="tap">
  701. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  702. <br />
  703. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  704. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  705. </p>
  706. <pre>
  707. _([1,2,3,200]).chain().
  708. select(function(num) { return num % 2 == 0; }).
  709. tap(console.log).
  710. map(function(num) { return num * num }).
  711. value();
  712. =&gt; [2, 200]
  713. =&gt; [4, 40000]
  714. </pre>
  715. <p id="isEqual">
  716. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  717. <br />
  718. Performs an optimized deep comparison between the two objects, to determine
  719. if they should be considered equal.
  720. </p>
  721. <pre>
  722. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  723. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  724. moe == clone;
  725. =&gt; false
  726. _.isEqual(moe, clone);
  727. =&gt; true
  728. </pre>
  729. <p id="isEmpty">
  730. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  731. <br />
  732. Returns <i>true</i> if <b>object</b> contains no values.
  733. </p>
  734. <pre>
  735. _.isEmpty([1, 2, 3]);
  736. =&gt; false
  737. _.isEmpty({});
  738. =&gt; true
  739. </pre>
  740. <p id="isElement">
  741. <b class="header">isElement</b><code>_.isElement(object)</code>
  742. <br />
  743. Returns <i>true</i> if <b>object</b> is a DOM element.
  744. </p>
  745. <pre>
  746. _.isElement(jQuery('body')[0]);
  747. =&gt; true
  748. </pre>
  749. <p id="isArray">
  750. <b class="header">isArray</b><code>_.isArray(object)</code>
  751. <br />
  752. Returns <i>true</i> if <b>object</b> is an Array.
  753. </p>
  754. <pre>
  755. (function(){ return _.isArray(arguments); })();
  756. =&gt; false
  757. _.isArray([1,2,3]);
  758. =&gt; true
  759. </pre>
  760. <p id="isArguments">
  761. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  762. <br />
  763. Returns <i>true</i> if <b>object</b> is an Arguments object.
  764. </p>
  765. <pre>
  766. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  767. =&gt; true
  768. _.isArguments([1,2,3]);
  769. =&gt; false
  770. </pre>
  771. <p id="isFunction">
  772. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  773. <br />
  774. Returns <i>true</i> if <b>object</b> is a Function.
  775. </p>
  776. <pre>
  777. _.isFunction(alert);
  778. =&gt; true
  779. </pre>
  780. <p id="isString">
  781. <b class="header">isString</b><code>_.isString(object)</code>
  782. <br />
  783. Returns <i>true</i> if <b>object</b> is a String.
  784. </p>
  785. <pre>
  786. _.isString("moe");
  787. =&gt; true
  788. </pre>
  789. <p id="isNumber">
  790. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  791. <br />
  792. Returns <i>true</i> if <b>object</b> is a Number.
  793. </p>
  794. <pre>
  795. _.isNumber(8.4 * 5);
  796. =&gt; true
  797. </pre>
  798. <p id="isBoolean">
  799. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  800. <br />
  801. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  802. </p>
  803. <pre>
  804. _.isBoolean(null);
  805. =&gt; false
  806. </pre>
  807. <p id="isDate">
  808. <b class="header">isDate</b><code>_.isDate(object)</code>
  809. <br />
  810. Returns <i>true</i> if <b>object</b> is a Date.
  811. </p>
  812. <pre>
  813. _.isDate(new Date());
  814. =&gt; true
  815. </pre>
  816. <p id="isRegExp">
  817. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  818. <br />
  819. Returns <i>true</i> if <b>object</b> is a RegExp.
  820. </p>
  821. <pre>
  822. _.isRegExp(/moe/);
  823. =&gt; true
  824. </pre>
  825. <p id="isNaN">
  826. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  827. <br />
  828. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  829. the same as the native <b>isNaN</b> function, which will also return
  830. true if the variable is <i>undefined</i>.
  831. </p>
  832. <pre>
  833. _.isNaN(NaN);
  834. =&gt; true
  835. isNaN(undefined);
  836. =&gt; true
  837. _.isNaN(undefined);
  838. =&gt; false
  839. </pre>
  840. <p id="isNull">
  841. <b class="header">isNull</b><code>_.isNull(object)</code>
  842. <br />
  843. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  844. </p>
  845. <pre>
  846. _.isNull(null);
  847. =&gt; true
  848. _.isNull(undefined);
  849. =&gt; false
  850. </pre>
  851. <p id="isUndefined">
  852. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  853. <br />
  854. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  855. </p>
  856. <pre>
  857. _.isUndefined(window.missingVariable);
  858. =&gt; true
  859. </pre>
  860. <h2>Utility Functions</h2>
  861. <p id="noConflict">
  862. <b class="header">noConflict</b><code>_.noConflict()</code>
  863. <br />
  864. Give control of the "_" variable back to its previous owner. Returns
  865. a reference to the <b>Underscore</b> object.
  866. </p>
  867. <pre>
  868. var underscore = _.noConflict();</pre>
  869. <p id="identity">
  870. <b class="header">identity</b><code>_.identity(value)</code>
  871. <br />
  872. Returns the same value that is used as the argument. In math:
  873. <tt>f(x) = x</tt><br />
  874. This function looks useless, but is used throughout Underscore as
  875. a default iterator.
  876. </p>
  877. <pre>
  878. var moe = {name : 'moe'};
  879. moe === _.identity(moe);
  880. =&gt; true</pre>
  881. <p id="times">
  882. <b class="header">times</b><code>_.times(n, iterator)</code>
  883. <br />
  884. Invokes the given iterator function <b>n</b> times.
  885. </p>
  886. <pre>
  887. _(3).times(function(){ genie.grantWish(); });</pre>
  888. <p id="breakLoop">
  889. <b class="header">breakLoop</b><code>_.breakLoop()</code>
  890. <br />
  891. Breaks out of the current loop iteration. Similar to the <tt>break</tt>
  892. keyword in regular "for" loop, but works within an iterator function.
  893. Uses the native <tt>StopIteration</tt> object in JavaScript 1.7 compliant
  894. browsers.
  895. </p>
  896. <pre>
  897. var result = null;
  898. _.each([1, 2, 3], function(num) {
  899. if ((result = num) == 2) _.breakLoop();
  900. });
  901. result;
  902. =&gt; 2</pre>
  903. <p id="mixin">
  904. <b class="header">mixin</b><code>_.mixin(object)</code>
  905. <br />
  906. Allows you to extend Underscore with your own utility functions. Pass
  907. a hash of <tt>{name: function}</tt> definitions to have your functions
  908. added to the Underscore object, as well as the OOP wrapper.
  909. </p>
  910. <pre>
  911. _.mixin({
  912. capitalize : function(string) {
  913. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  914. }
  915. });
  916. _("fabio").capitalize();
  917. =&gt; "Fabio"
  918. </pre>
  919. <p id="uniqueId">
  920. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  921. <br />
  922. Generate a globally-unique id for client-side models or DOM elements
  923. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  924. </p>
  925. <pre>
  926. _.uniqueId('contact_');
  927. =&gt; 'contact_104'</pre>
  928. <p id="template">
  929. <b class="header">template</b><code>_.template(templateString, [context])</code>
  930. <br />
  931. Compiles JavaScript templates into functions that can be evaluated
  932. for rendering. Useful for rendering complicated bits of HTML from JSON
  933. data sources. Template functions can both interpolate variables, using<br />
  934. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  935. <tt>&lt;% &hellip; %&gt;</tt>. When you evaluate a template function, pass in a
  936. <b>context</b> object that has properties corresponding to the template's free
  937. variables. If you're writing a one-off, you can pass the <b>context</b>
  938. object as the second parameter to <b>template</b> in order to render
  939. immediately instead of returning a template function.
  940. </p>
  941. <pre>
  942. var compiled = _.template("hello: &lt;%= name %&gt;");
  943. compiled({name : 'moe'});
  944. =&gt; "hello: moe"
  945. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  946. _.template(list, {people : ['moe', 'curly', 'larry']});
  947. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"</pre>
  948. <p>
  949. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  950. template settings to use different symbols to set off interpolated code.
  951. Define <b>start</b> and <b>end</b> tokens, and an <b>interpolate</b> regex
  952. to match expressions that should be evaluated and inserted. For example,
  953. to perform
  954. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  955. style templating:
  956. </p>
  957. <pre>
  958. _.templateSettings = {
  959. start : '{{',
  960. end : '}}',
  961. interpolate : /\{\{(.+?)\}\}/g
  962. };
  963. var template = _.template("Hello {{ name }}!");
  964. template({name : "Mustache"});
  965. =&gt; "Hello Mustache!"</pre>
  966. <h2>Chaining</h2>
  967. <p id="chain">
  968. <b class="header">chain</b><code>_(obj).chain()</code>
  969. <br />
  970. Returns a wrapped object. Calling methods on this object will continue
  971. to return wrapped objects until <tt>value</tt> is used. (
  972. <a href="#styles">A more realistic example.</a>)
  973. </p>
  974. <pre>
  975. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  976. var youngest = _(stooges).chain()
  977. .sortBy(function(stooge){ return stooge.age; })
  978. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  979. .first()
  980. .value();
  981. =&gt; "moe is 21"
  982. </pre>
  983. <p id="value">
  984. <b class="header">value</b><code>_(obj).value()</code>
  985. <br />
  986. Extracts the value of a wrapped object.
  987. </p>
  988. <pre>
  989. _([1, 2, 3]).value();
  990. =&gt; [1, 2, 3]
  991. </pre>
  992. <h2 id="duck_typing">Duck Typing</h2>
  993. <p>
  994. The <b>isType</b> (<tt>isArray</tt>, <tt>isFunction</tt>, <tt>isString</tt> ...) family of type-checking
  995. functions use property detection to do their work, which, although
  996. orders of magnitude faster than the alternative, isn't entirely safe when dealing
  997. with objects that are used as hashes, where arbitrary strings are being
  998. set for the keys. It's entirely possible for an object to masquerade as
  999. another type, if you're setting properties with names like "concat" and
  1000. "charCodeAt". So be aware.
  1001. </p>
  1002. <h2>Links &amp; Suggested Reading</h2>
  1003. <p>
  1004. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  1005. a Lua port of the functions that are applicable in both languages.
  1006. Includes OOP-wrapping and chaining.
  1007. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1008. available on GitHub.
  1009. </p>
  1010. <p>
  1011. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1012. </p>
  1013. <p>
  1014. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1015. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1016. </p>
  1017. <p>
  1018. Oliver Steele's
  1019. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1020. which includes comprehensive higher-order function support as well as string lambdas.
  1021. </p>
  1022. <p>
  1023. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1024. </p>
  1025. <h2>Change Log</h2>
  1026. <p>
  1027. <b class="header">1.0.2</b><br />
  1028. Fixes <tt>_.isArguments</tt> in recent versions of Opera, which have
  1029. arguments objects as real Arrays.
  1030. </p>
  1031. <p>
  1032. <b class="header">1.0.1</b><br />
  1033. Bugfix for <tt>_.isEqual</tt>, when comparing two objects with the same
  1034. number of undefined keys, but with different names.
  1035. </p>
  1036. <p>
  1037. <b class="header">1.0.0</b><br />
  1038. Things have been stable for many months now, so Underscore is now
  1039. considered to be out of beta, at <b>1.0</b>. Improvements since <b>0.6</b>
  1040. include <tt>_.isBoolean</tt>, and the ability to have <tt>_.extend</tt>
  1041. take multiple source objects.
  1042. </p>
  1043. <p>
  1044. <b class="header">0.6.0</b><br />
  1045. Major release. Incorporates a number of
  1046. <a href="http://github.com/ratbeard">Mile Frawley's</a> refactors for
  1047. safer duck-typing on collection functions, and cleaner internals. A new
  1048. <tt>_.mixin</tt> method that allows you to extend Underscore with utility
  1049. functions of your own. Added <tt>_.times</tt>, which works the same as in
  1050. Ruby or Prototype.js. Native support for ECMAScript 5's <tt>Array.isArray</tt>,
  1051. and <tt>Object.keys</tt>.
  1052. </p>
  1053. <p>
  1054. <b class="header">0.5.8</b><br />
  1055. Fixed Underscore's collection functions to work on
  1056. <a href="https://developer.mozilla.org/En/DOM/NodeList">NodeLists</a> and
  1057. <a href="https://developer.mozilla.org/En/DOM/HTMLCollection">HTMLCollections</a>
  1058. once more, thanks to
  1059. <a href="http://github.com/jmtulloss">Justin Tulloss</a>.
  1060. </p>
  1061. <p>
  1062. <b class="header">0.5.7</b><br />
  1063. A safer implementation of <tt>_.isArguments</tt>, and a
  1064. faster <tt>_.isNumber</tt>,<br />thanks to
  1065. <a href="http://jedschmidt.com/">Jed Schmidt</a>.
  1066. </p>
  1067. <p>
  1068. <b class="header">0.5.6</b><br />
  1069. Customizable delimiters for <tt>_.template</tt>, contributed by
  1070. <a href="http://github.com/iamnoah">Noah Sloan</a>.
  1071. </p>
  1072. <p>
  1073. <b class="header">0.5.5</b><br />
  1074. Fix for a bug in MobileSafari's OOP-wrapper, with the arguments object.
  1075. </p>
  1076. <p>
  1077. <b class="header">0.5.4</b><br />
  1078. Fix for multiple single quotes within a template string for
  1079. <tt>_.template</tt>. See:
  1080. <a href="http://www.west-wind.com/Weblog/posts/509108.aspx">Rick Strahl's blog post</a>.
  1081. </p>
  1082. <p>
  1083. <b class="header">0.5.2</b><br />
  1084. New implementations of <tt>isArray</tt>, <tt>isDate</tt>, <tt>isFunction</tt>,
  1085. <tt>isNumber</tt>, <tt>isRegExp</tt>, and <tt>isString</tt>, thanks to
  1086. a suggestion from
  1087. <a href="http://www.broofa.com/">Robert Kieffer</a>.
  1088. Instead of doing <tt>Object#toString</tt>
  1089. comparisons, they now check for expected properties, which is less safe,
  1090. but more than an order of magnitude faster. Most other Underscore
  1091. functions saw minor speed improvements as a result.
  1092. <a href="http://dolzhenko.org/">Evgeniy Dolzhenko</a>
  1093. contributed <tt>_.tap</tt>,
  1094. <a href="http://ruby-doc.org/core-1.9/classes/Object.html#M000191">similar to Ruby 1.9's</a>,
  1095. which is handy for injecting side effects (like logging) into chained calls.
  1096. </p>
  1097. <p>
  1098. <b class="header">0.5.1</b><br />
  1099. Added an <tt>_.isArguments</tt> function. Lots of little safety checks
  1100. and optimizations contributed by
  1101. <a href="http://github.com/iamnoah/">Noah Sloan</a> and Andri Möll.
  1102. </p>
  1103. <p>
  1104. <b class="header">0.5.0</b><br />
  1105. <b>[API Changes]</b> <tt>_.bindAll</tt> now takes the context object as
  1106. its first parameter. If no method names are passed, all of the context
  1107. object's methods are bound to it, enabling chaining and easier binding.
  1108. <tt>_.functions</tt> now takes a single argument and returns the names
  1109. of its Function properties. Calling <tt>_.functions(_)</tt> will get you
  1110. the previous behavior.
  1111. Added <tt>_.isRegExp</tt> so that <tt>isEqual</tt> can now test for RegExp equality.
  1112. All of the "is" functions have been shrunk down into a single definition.
  1113. <a href="http://github.com/grayrest/">Karl Guertin</a> contributed patches.
  1114. </p>
  1115. <p>
  1116. <b class="header">0.4.7</b><br />
  1117. Added <tt>isDate</tt>, <tt>isNaN</tt>, and <tt>isNull</tt>, for completeness.
  1118. Optimizations for <tt>isEqual</tt> when checking equality between Arrays
  1119. or Dates. <tt>_.keys</tt> is now <small><i><b>25%&ndash;2X</b></i></small> faster (depending on your
  1120. browser) which speeds up the functions that rely on it, such as <tt>_.each</tt>.
  1121. </p>
  1122. <p>
  1123. <b class="header">0.4.6</b><br />
  1124. Added the <tt>range</tt> function, a port of the
  1125. <a href="http://docs.python.org/library/functions.html#range">Python
  1126. function of the same name</a>, for generating flexibly-numbered lists
  1127. of integers. Original patch contributed by
  1128. <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
  1129. </p>
  1130. <p>
  1131. <b class="header">0.4.5</b><br />
  1132. Added <tt>rest</tt> for Arrays and arguments objects, and aliased
  1133. <tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
  1134. thanks to <a href="http://github.com/lukesutton/">Luke Sutton</a>'s patches.
  1135. Added tests ensuring that all Underscore Array functions also work on
  1136. <i>arguments</i> objects.
  1137. </p>
  1138. <p>
  1139. <b class="header">0.4.4</b><br />
  1140. Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
  1141. <tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
  1142. </p>
  1143. <p>
  1144. <b class="header">0.4.3</b><br />
  1145. Started using the native <tt>StopIteration</tt> object in browsers that support it.
  1146. Fixed Underscore setup for CommonJS environments.
  1147. </p>
  1148. <p>
  1149. <b class="header">0.4.2</b><br />
  1150. Renamed the unwrapping function to <tt>value</tt>, for clarity.
  1151. </p>
  1152. <p>
  1153. <b class="header">0.4.1</b><br />
  1154. Chained Underscore objects now support the Array prototype methods, so
  1155. that you can perform the full range of operations on a wrapped array
  1156. without having to break your chain. Added a <tt>breakLoop</tt> method
  1157. to <b>break</b> in the middle of any Underscore iteration. Added an
  1158. <tt>isEmpty</tt> function that works on arrays and objects.
  1159. </p>
  1160. <p>
  1161. <b class="header">0.4.0</b><br />
  1162. All Underscore functions can now be called in an object-oriented style,
  1163. like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
  1164. <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
  1165. Wrapped objects can be chained through multiple
  1166. method invocations. A <a href="#functions"><tt>functions</tt></a> method
  1167. was added, providing a sorted list of all the functions in Underscore.
  1168. </p>
  1169. <p>
  1170. <b class="header">0.3.3</b><br />
  1171. Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
  1172. as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
  1173. </p>
  1174. <p>
  1175. <b class="header">0.3.2</b><br />
  1176. Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
  1177. interpreters with: <tt>load("underscore.js")</tt>.
  1178. Added <a href="#identity"><tt>identity</tt></a> as a utility function.
  1179. </p>
  1180. <p>
  1181. <b class="header">0.3.1</b><br />
  1182. All iterators are now passed in the original collection as their third
  1183. argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
  1184. objects is now called with <tt>(value, key, collection)</tt>, for details
  1185. see <a href="#each"><tt>_.each</tt></a>.
  1186. </p>
  1187. <p>
  1188. <b class="header">0.3.0</b><br />
  1189. Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
  1190. comprehensive optimizations, merged in
  1191. <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
  1192. <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
  1193. <a href="http://narwhaljs.org/">Narwhal</a> compliant.
  1194. </p>
  1195. <p>
  1196. <b class="header">0.2.0</b><br />
  1197. Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
  1198. <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
  1199. <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
  1200. </p>
  1201. <p>
  1202. <b class="header">0.1.1</b><br />
  1203. Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
  1204. other variables.
  1205. </p>
  1206. <p>
  1207. <b class="header">0.1.0</b><br />
  1208. Initial release of Underscore.js.
  1209. </p>
  1210. <p>
  1211. <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
  1212. <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
  1213. </a>
  1214. </p>
  1215. </div>
  1216. </div>
  1217. <!-- Include Underscore, so you can play with it in the console. -->
  1218. <script type="text/javascript" src="underscore.js"></script>
  1219. </body>
  1220. </html>