PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/underscore/index.html

https://bitbucket.org/worldsayshi/master_thesis-gui
HTML | 2382 lines | 2168 code | 213 blank | 1 comment | 0 complexity | 6a23b26a6763122aa1bebc1d2f157396 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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. <meta name="viewport" content="width=device-width" />
  7. <link rel="canonical" href="http://underscorejs.org" />
  8. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
  9. <title>Underscore.js</title>
  10. <style>
  11. body {
  12. font-size: 14px;
  13. line-height: 22px;
  14. background: #f4f4f4 url(docs/images/background.png);
  15. color: #000;
  16. font-family: Helvetica Neue, Helvetica, Arial;
  17. }
  18. .interface {
  19. font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
  20. }
  21. div#sidebar {
  22. background: #fff;
  23. position: fixed;
  24. top: 0; left: 0; bottom: 0;
  25. width: 200px;
  26. overflow-y: auto;
  27. overflow-x: hidden;
  28. -webkit-overflow-scrolling: touch;
  29. padding: 15px 0 30px 30px;
  30. border-right: 1px solid #bbb;
  31. box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
  32. }
  33. a.toc_title, a.toc_title:visited {
  34. display: block;
  35. color: black;
  36. font-weight: bold;
  37. margin-top: 15px;
  38. }
  39. a.toc_title:hover {
  40. text-decoration: underline;
  41. }
  42. #sidebar .version {
  43. font-size: 10px;
  44. font-weight: normal;
  45. }
  46. ul.toc_section {
  47. font-size: 11px;
  48. line-height: 14px;
  49. margin: 5px 0 0 0;
  50. padding-left: 0px;
  51. list-style-type: none;
  52. font-family: Lucida Grande;
  53. }
  54. .toc_section li {
  55. cursor: pointer;
  56. margin: 0 0 3px 0;
  57. }
  58. .toc_section li a {
  59. text-decoration: none;
  60. color: black;
  61. }
  62. .toc_section li a:hover {
  63. text-decoration: underline;
  64. }
  65. div.container {
  66. width: 550px;
  67. margin: 40px 0 50px 260px;
  68. }
  69. img#logo {
  70. width: 396px;
  71. height: 69px;
  72. }
  73. div.warning {
  74. margin-top: 15px;
  75. font: bold 11px Arial;
  76. color: #770000;
  77. }
  78. p {
  79. margin: 20px 0;
  80. width: 550px;
  81. }
  82. a, a:visited {
  83. color: #444;
  84. }
  85. a:active, a:hover {
  86. color: #000;
  87. }
  88. h1, h2, h3, h4, h5, h6 {
  89. padding-top: 20px;
  90. }
  91. h2 {
  92. font-size: 20px;
  93. }
  94. b.header {
  95. font-size: 16px;
  96. line-height: 30px;
  97. }
  98. span.alias {
  99. font-size: 14px;
  100. font-style: italic;
  101. margin-left: 20px;
  102. }
  103. table, tr, td {
  104. margin: 0; padding: 0;
  105. }
  106. td {
  107. padding: 2px 12px 2px 0;
  108. }
  109. table .rule {
  110. height: 1px;
  111. background: #ccc;
  112. margin: 5px 0;
  113. }
  114. ul {
  115. list-style-type: circle;
  116. padding: 0 0 0 20px;
  117. }
  118. li {
  119. width: 500px;
  120. margin-bottom: 10px;
  121. }
  122. code, pre, tt {
  123. font-family: Monaco, Consolas, "Lucida Console", monospace;
  124. font-size: 12px;
  125. line-height: 18px;
  126. font-style: normal;
  127. }
  128. tt {
  129. padding: 0px 3px;
  130. background: #fff;
  131. border: 1px solid #ddd;
  132. zoom: 1;
  133. }
  134. code {
  135. margin-left: 20px;
  136. }
  137. pre {
  138. font-size: 12px;
  139. padding: 2px 0 2px 15px;
  140. border-left: 5px solid #bbb;
  141. margin: 0px 0 30px;
  142. }
  143. @media only screen and (-webkit-min-device-pixel-ratio: 1.5) and (max-width: 640px),
  144. only screen and (-o-min-device-pixel-ratio: 3/2) and (max-width: 640px),
  145. only screen and (min-device-pixel-ratio: 1.5) and (max-width: 640px) {
  146. img {
  147. max-width: 100%;
  148. }
  149. div#sidebar {
  150. -webkit-overflow-scrolling: initial;
  151. position: relative;
  152. width: 90%;
  153. height: 120px;
  154. left: 0;
  155. top: -7px;
  156. padding: 10px 0 10px 30px;
  157. border: 0;
  158. }
  159. img#logo {
  160. width: auto;
  161. height: auto;
  162. }
  163. div.container {
  164. margin: 0;
  165. width: 100%;
  166. }
  167. p, div.container ul {
  168. max-width: 98%;
  169. overflow-x: scroll;
  170. }
  171. pre {
  172. overflow: scroll;
  173. }
  174. }
  175. </style>
  176. </head>
  177. <body>
  178. <div id="sidebar" class="interface">
  179. <a class="toc_title" href="#">
  180. Underscore.js <span class="version">(1.4.2)</span>
  181. </a>
  182. <a class="toc_title" href="#">
  183. Introduction
  184. </a>
  185. <a class="toc_title" href="#collections">
  186. Collections
  187. </a>
  188. <ul class="toc_section">
  189. <li>- <a href="#each">each</a></li>
  190. <li>- <a href="#map">map</a></li>
  191. <li>- <a href="#reduce">reduce</a></li>
  192. <li>- <a href="#reduceRight">reduceRight</a></li>
  193. <li>- <a href="#find">find</a></li>
  194. <li>- <a href="#filter">filter</a></li>
  195. <li>- <a href="#where">where</a></li>
  196. <li>- <a href="#reject">reject</a></li>
  197. <li>- <a href="#every">every</a></li>
  198. <li>- <a href="#some">some</a></li>
  199. <li>- <a href="#contains">contains</a></li>
  200. <li>- <a href="#invoke">invoke</a></li>
  201. <li>- <a href="#pluck">pluck</a></li>
  202. <li>- <a href="#max">max</a></li>
  203. <li>- <a href="#min">min</a></li>
  204. <li>- <a href="#sortBy">sortBy</a></li>
  205. <li>- <a href="#groupBy">groupBy</a></li>
  206. <li>- <a href="#countBy">countBy</a></li>
  207. <li>- <a href="#shuffle">shuffle</a></li>
  208. <li>- <a href="#toArray">toArray</a></li>
  209. <li>- <a href="#size">size</a></li>
  210. </ul>
  211. <a class="toc_title" href="#arrays">
  212. Arrays
  213. </a>
  214. <ul class="toc_section">
  215. <li>- <a href="#first">first</a></li>
  216. <li>- <a href="#initial">initial</a></li>
  217. <li>- <a href="#last">last</a></li>
  218. <li>- <a href="#rest">rest</a></li>
  219. <li>- <a href="#compact">compact</a></li>
  220. <li>- <a href="#flatten">flatten</a></li>
  221. <li>- <a href="#without">without</a></li>
  222. <li>- <a href="#union">union</a></li>
  223. <li>- <a href="#intersection">intersection</a></li>
  224. <li>- <a href="#difference">difference</a></li>
  225. <li>- <a href="#uniq">uniq</a></li>
  226. <li>- <a href="#zip">zip</a></li>
  227. <li>- <a href="#object">object</a></li>
  228. <li>- <a href="#indexOf">indexOf</a></li>
  229. <li>- <a href="#lastIndexOf">lastIndexOf</a></li>
  230. <li>- <a href="#sortedIndex">sortedIndex</a></li>
  231. <li>- <a href="#range">range</a></li>
  232. </ul>
  233. <a class="toc_title" href="#functions">
  234. Functions
  235. </a>
  236. <ul class="toc_section">
  237. <li>- <a href="#bind">bind</a></li>
  238. <li>- <a href="#bindAll">bindAll</a></li>
  239. <li>- <a href="#memoize">memoize</a></li>
  240. <li>- <a href="#delay">delay</a></li>
  241. <li>- <a href="#defer">defer</a></li>
  242. <li>- <a href="#throttle">throttle</a></li>
  243. <li>- <a href="#debounce">debounce</a></li>
  244. <li>- <a href="#once">once</a></li>
  245. <li>- <a href="#after">after</a></li>
  246. <li>- <a href="#wrap">wrap</a></li>
  247. <li>- <a href="#compose">compose</a></li>
  248. </ul>
  249. <a class="toc_title" href="#objects">
  250. Objects
  251. </a>
  252. <ul class="toc_section">
  253. <li>- <a href="#keys">keys</a></li>
  254. <li>- <a href="#values">values</a></li>
  255. <li>- <a href="#pairs">pairs</a></li>
  256. <li>- <a href="#invert">invert</a></li>
  257. <li>- <a href="#object-functions">functions</a></li>
  258. <li>- <a href="#extend">extend</a></li>
  259. <li>- <a href="#pick">pick</a></li>
  260. <li>- <a href="#omit">omit</a></li>
  261. <li>- <a href="#defaults">defaults</a></li>
  262. <li>- <a href="#clone">clone</a></li>
  263. <li>- <a href="#tap">tap</a></li>
  264. <li>- <a href="#has">has</a></li>
  265. <li>- <a href="#isEqual">isEqual</a></li>
  266. <li>- <a href="#isEmpty">isEmpty</a></li>
  267. <li>- <a href="#isElement">isElement</a></li>
  268. <li>- <a href="#isArray">isArray</a></li>
  269. <li>- <a href="#isObject">isObject</a></li>
  270. <li>- <a href="#isArguments">isArguments</a></li>
  271. <li>- <a href="#isFunction">isFunction</a></li>
  272. <li>- <a href="#isString">isString</a></li>
  273. <li>- <a href="#isNumber">isNumber</a></li>
  274. <li>- <a href="#isFinite">isFinite</a></li>
  275. <li>- <a href="#isBoolean">isBoolean</a></li>
  276. <li>- <a href="#isDate">isDate</a></li>
  277. <li>- <a href="#isRegExp">isRegExp</a></li>
  278. <li>- <a href="#isNaN">isNaN</a></li>
  279. <li>- <a href="#isNull">isNull</a></li>
  280. <li>- <a href="#isUndefined">isUndefined</a></li>
  281. </ul>
  282. <a class="toc_title" href="#utility">
  283. Utility
  284. </a>
  285. <ul class="toc_section">
  286. <li>- <a href="#noConflict">noConflict</a></li>
  287. <li>- <a href="#identity">identity</a></li>
  288. <li>- <a href="#times">times</a></li>
  289. <li>- <a href="#random">random</a></li>
  290. <li>- <a href="#mixin">mixin</a></li>
  291. <li>- <a href="#uniqueId">uniqueId</a></li>
  292. <li>- <a href="#escape">escape</a></li>
  293. <li>- <a href="#unescape">unescape</a></li>
  294. <li>- <a href="#result">result</a></li>
  295. <li>- <a href="#template">template</a></li>
  296. </ul>
  297. <a class="toc_title" href="#chaining">
  298. Chaining
  299. </a>
  300. <ul class="toc_section">
  301. <li>- <a href="#chain">chain</a></li>
  302. <li>- <a href="#value">value</a></li>
  303. </ul>
  304. <a class="toc_title" href="#links">
  305. Links
  306. </a>
  307. <a class="toc_title" href="#changelog">
  308. Change Log
  309. </a>
  310. </div>
  311. <div class="container">
  312. <p id="introduction">
  313. <img id="logo" src="docs/images/underscore.png" alt="Underscore.js" />
  314. </p>
  315. <p>
  316. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  317. utility-belt library for JavaScript that provides a lot of the
  318. functional programming support that you would expect in
  319. <a href="http://prototypejs.org/api">Prototype.js</a>
  320. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  321. but without extending any of the built-in JavaScript objects. It's the
  322. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
  323. and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
  324. </p>
  325. <p>
  326. Underscore provides 80-odd functions that support both the usual
  327. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  328. as well as more specialized helpers: function binding, javascript
  329. templating, deep equality testing, and so on. It delegates to built-in
  330. functions, if present, so modern browsers will use the
  331. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  332. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  333. </p>
  334. <p>
  335. A complete <a href="test/">Test &amp; Benchmark Suite</a>
  336. is included for your perusal.
  337. </p>
  338. <p>
  339. You may also read through the <a href="docs/underscore.html">annotated source code</a>.
  340. </p>
  341. <p>
  342. The project is
  343. <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
  344. You can report bugs and discuss features on the
  345. <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
  346. on Freenode in the <tt>#documentcloud</tt> channel,
  347. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  348. </p>
  349. <p>
  350. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  351. </p>
  352. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  353. <table>
  354. <tr>
  355. <td><a href="underscore.js">Development Version (1.4.2)</a></td>
  356. <td><i>40kb, Uncompressed with Plentiful Comments</i></td>
  357. </tr>
  358. <tr>
  359. <td><a href="underscore-min.js">Production Version (1.4.2)</a></td>
  360. <td><i>4kb, Minified and Gzipped</i></td>
  361. </tr>
  362. <tr>
  363. <td colspan="2"><div class="rule"></div></td>
  364. </tr>
  365. <tr>
  366. <td><a href="https://raw.github.com/documentcloud/underscore/master/underscore.js">Edge Version</a></td>
  367. <td><i>Unreleased, current <tt>master</tt>, use at your own risk</i></td>
  368. </tr>
  369. </table>
  370. <div id="documentation">
  371. <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
  372. <p id="each">
  373. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  374. <span class="alias">Alias: <b>forEach</b></span>
  375. <br />
  376. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  377. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  378. passed. Each invocation of <b>iterator</b> is called with three arguments:
  379. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  380. arguments will be <tt>(value, key, list)</tt>. Delegates to the native
  381. <b>forEach</b> function if it exists.
  382. </p>
  383. <pre>
  384. _.each([1, 2, 3], function(num){ alert(num); });
  385. =&gt; alerts each number in turn...
  386. _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
  387. =&gt; alerts each number in turn...</pre>
  388. <p id="map">
  389. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  390. <span class="alias">Alias: <b>collect</b></span>
  391. <br />
  392. Produces a new array of values by mapping each value in <b>list</b>
  393. through a transformation function (<b>iterator</b>). If the native <b>map</b> method
  394. exists, it will be used instead. If <b>list</b> is a JavaScript object,
  395. <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
  396. </p>
  397. <pre>
  398. _.map([1, 2, 3], function(num){ return num * 3; });
  399. =&gt; [3, 6, 9]
  400. _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
  401. =&gt; [3, 6, 9]</pre>
  402. <p id="reduce">
  403. <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
  404. <span class="alias">Aliases: <b>inject, foldl</b></span>
  405. <br />
  406. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  407. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  408. of the reduction, and each successive step of it should be returned by
  409. <b>iterator</b>. The iterator is passed four arguments: the <tt>memo</tt>,
  410. then the <tt>value</tt> and <tt>index</tt> (or key) of the iteration,
  411. and finally a reference to the entire <tt>list</tt>.
  412. </p>
  413. <pre>
  414. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  415. =&gt; 6
  416. </pre>
  417. <p id="reduceRight">
  418. <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
  419. <span class="alias">Alias: <b>foldr</b></span>
  420. <br />
  421. The right-associative version of <b>reduce</b>. Delegates to the
  422. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  423. is not as useful in JavaScript as it would be in a language with lazy
  424. evaluation.
  425. </p>
  426. <pre>
  427. var list = [[0, 1], [2, 3], [4, 5]];
  428. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  429. =&gt; [4, 5, 2, 3, 0, 1]
  430. </pre>
  431. <p id="find">
  432. <b class="header">find</b><code>_.find(list, iterator, [context])</code>
  433. <span class="alias">Alias: <b>detect</b></span>
  434. <br />
  435. Looks through each value in the <b>list</b>, returning the first one that
  436. passes a truth test (<b>iterator</b>). The function returns as
  437. soon as it finds an acceptable element, and doesn't traverse the
  438. entire list.
  439. </p>
  440. <pre>
  441. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  442. =&gt; 2
  443. </pre>
  444. <p id="filter">
  445. <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
  446. <span class="alias">Alias: <b>select</b></span>
  447. <br />
  448. Looks through each value in the <b>list</b>, returning an array of all
  449. the values that pass a truth test (<b>iterator</b>). Delegates to the
  450. native <b>filter</b> method, if it exists.
  451. </p>
  452. <pre>
  453. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  454. =&gt; [2, 4, 6]
  455. </pre>
  456. <p id="where">
  457. <b class="header">where</b><code>_.where(list, properties)</code>
  458. <br />
  459. Looks through each value in the <b>list</b>, returning an array of all
  460. the values that contain all of the key-value pairs listed in <b>properties</b>.
  461. </p>
  462. <pre>
  463. _.where(listOfPlays, {author: "Shakespeare", year: 1611});
  464. =&gt; [{title: "Cymbeline", author: "Shakespeare", year: 1611},
  465. {title: "The Tempest", author: "Shakespeare", year: 1611}]
  466. </pre>
  467. <p id="reject">
  468. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  469. <br />
  470. Returns the values in <b>list</b> without the elements that the truth
  471. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
  472. </p>
  473. <pre>
  474. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  475. =&gt; [1, 3, 5]
  476. </pre>
  477. <p id="every">
  478. <b class="header">every</b><code>_.every(list, iterator, [context])</code>
  479. <span class="alias">Alias: <b>all</b></span>
  480. <br />
  481. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  482. truth test. Delegates to the native method <b>every</b>, if present.
  483. </p>
  484. <pre>
  485. _.every([true, 1, null, 'yes'], _.identity);
  486. =&gt; false
  487. </pre>
  488. <p id="some">
  489. <b class="header">some</b><code>_.some(list, [iterator], [context])</code>
  490. <span class="alias">Alias: <b>any</b></span>
  491. <br />
  492. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  493. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  494. if a true element is found. Delegates to the native method <b>some</b>,
  495. if present.
  496. </p>
  497. <pre>
  498. _.some([null, 0, 'yes', false]);
  499. =&gt; true
  500. </pre>
  501. <p id="contains">
  502. <b class="header">contains</b><code>_.contains(list, value)</code>
  503. <span class="alias">Alias: <b>include</b></span>
  504. <br />
  505. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>.
  506. Uses <b>indexOf</b> internally, if <b>list</b> is an Array.
  507. </p>
  508. <pre>
  509. _.contains([1, 2, 3], 3);
  510. =&gt; true
  511. </pre>
  512. <p id="invoke">
  513. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  514. <br />
  515. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  516. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  517. method invocation.
  518. </p>
  519. <pre>
  520. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  521. =&gt; [[1, 5, 7], [1, 2, 3]]
  522. </pre>
  523. <p id="pluck">
  524. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  525. <br />
  526. A convenient version of what is perhaps the most common use-case for
  527. <b>map</b>: extracting a list of property values.
  528. </p>
  529. <pre>
  530. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  531. _.pluck(stooges, 'name');
  532. =&gt; ["moe", "larry", "curly"]
  533. </pre>
  534. <p id="max">
  535. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  536. <br />
  537. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  538. it will be used on each value to generate the criterion by which the
  539. value is ranked.
  540. </p>
  541. <pre>
  542. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  543. _.max(stooges, function(stooge){ return stooge.age; });
  544. =&gt; {name : 'curly', age : 60};
  545. </pre>
  546. <p id="min">
  547. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  548. <br />
  549. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  550. it will be used on each value to generate the criterion by which the
  551. value is ranked.
  552. </p>
  553. <pre>
  554. var numbers = [10, 5, 100, 2, 1000];
  555. _.min(numbers);
  556. =&gt; 2
  557. </pre>
  558. <p id="sortBy">
  559. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  560. <br />
  561. Returns a sorted copy of <b>list</b>, ranked in ascending order by the
  562. results of running each value through <b>iterator</b>. Iterator may
  563. also be the string name of the property to sort by (eg. <tt>length</tt>).
  564. </p>
  565. <pre>
  566. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  567. =&gt; [5, 4, 6, 3, 1, 2]
  568. </pre>
  569. <p id="groupBy">
  570. <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
  571. <br />
  572. Splits a collection into sets, grouped by the result of running each
  573. value through <b>iterator</b>. If <b>iterator</b> is a string instead of
  574. a function, groups by the property named by <b>iterator</b> on each of
  575. the values.
  576. </p>
  577. <pre>
  578. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  579. =&gt; {1: [1.3], 2: [2.1, 2.4]}
  580. _.groupBy(['one', 'two', 'three'], 'length');
  581. =&gt; {3: ["one", "two"], 5: ["three"]}
  582. </pre>
  583. <p id="countBy">
  584. <b class="header">countBy</b><code>_.countBy(list, iterator)</code>
  585. <br />
  586. Sorts a list into groups and returns a count for the number of objects
  587. in each group.
  588. Similar to <tt>groupBy</tt>, but instead of returning a list of values,
  589. returns a count for the number of values in that group.
  590. </p>
  591. <pre>
  592. _.countBy([1, 2, 3, 4, 5], function(num) {
  593. return num % 2 == 0 ? 'even' : 'odd';
  594. });
  595. =&gt; {odd: 3, even: 2}
  596. </pre>
  597. <p id="shuffle">
  598. <b class="header">shuffle</b><code>_.shuffle(list)</code>
  599. <br />
  600. Returns a shuffled copy of the <b>list</b>, using a version of the
  601. <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
  602. </p>
  603. <pre>
  604. _.shuffle([1, 2, 3, 4, 5, 6]);
  605. =&gt; [4, 1, 6, 3, 5, 2]
  606. </pre>
  607. <p id="toArray">
  608. <b class="header">toArray</b><code>_.toArray(list)</code>
  609. <br />
  610. Converts the <b>list</b> (anything that can be iterated over), into a
  611. real Array. Useful for transmuting the <b>arguments</b> object.
  612. </p>
  613. <pre>
  614. (function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  615. =&gt; [2, 3, 4]
  616. </pre>
  617. <p id="size">
  618. <b class="header">size</b><code>_.size(list)</code>
  619. <br />
  620. Return the number of values in the <b>list</b>.
  621. </p>
  622. <pre>
  623. _.size({one : 1, two : 2, three : 3});
  624. =&gt; 3
  625. </pre>
  626. <h2 id="arrays">Array Functions</h2>
  627. <p>
  628. <i>
  629. Note: All array functions will also work on the <b>arguments</b> object.
  630. However, Underscore functions are not designed to work on "sparse" arrays.
  631. </i>
  632. </p>
  633. <p id="first">
  634. <b class="header">first</b><code>_.first(array, [n])</code>
  635. <span class="alias">Alias: <b>head</b>, <b>take</b></span>
  636. <br />
  637. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  638. return the first <b>n</b> elements of the array.
  639. </p>
  640. <pre>
  641. _.first([5, 4, 3, 2, 1]);
  642. =&gt; 5
  643. </pre>
  644. <p id="initial">
  645. <b class="header">initial</b><code>_.initial(array, [n])</code>
  646. <br />
  647. Returns everything but the last entry of the array. Especially useful on
  648. the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
  649. from the result.
  650. </p>
  651. <pre>
  652. _.initial([5, 4, 3, 2, 1]);
  653. =&gt; [5, 4, 3, 2]
  654. </pre>
  655. <p id="last">
  656. <b class="header">last</b><code>_.last(array, [n])</code>
  657. <br />
  658. Returns the last element of an <b>array</b>. Passing <b>n</b> will return
  659. the last <b>n</b> elements of the array.
  660. </p>
  661. <pre>
  662. _.last([5, 4, 3, 2, 1]);
  663. =&gt; 1
  664. </pre>
  665. <p id="rest">
  666. <b class="header">rest</b><code>_.rest(array, [index])</code>
  667. <span class="alias">Alias: <b>tail, drop</b></span>
  668. <br />
  669. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  670. to return the values of the array from that index onward.
  671. </p>
  672. <pre>
  673. _.rest([5, 4, 3, 2, 1]);
  674. =&gt; [4, 3, 2, 1]
  675. </pre>
  676. <p id="compact">
  677. <b class="header">compact</b><code>_.compact(array)</code>
  678. <br />
  679. Returns a copy of the <b>array</b> with all falsy values removed.
  680. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  681. <i>undefined</i> and <i>NaN</i> are all falsy.
  682. </p>
  683. <pre>
  684. _.compact([0, 1, false, 2, '', 3]);
  685. =&gt; [1, 2, 3]
  686. </pre>
  687. <p id="flatten">
  688. <b class="header">flatten</b><code>_.flatten(array, [shallow])</code>
  689. <br />
  690. Flattens a nested <b>array</b> (the nesting can be to any depth). If you
  691. pass <b>shallow</b>, the array will only be flattened a single level.
  692. </p>
  693. <pre>
  694. _.flatten([1, [2], [3, [[4]]]]);
  695. =&gt; [1, 2, 3, 4];
  696. _.flatten([1, [2], [3, [[4]]]], true);
  697. =&gt; [1, 2, 3, [[4]]];
  698. </pre>
  699. <p id="without">
  700. <b class="header">without</b><code>_.without(array, [*values])</code>
  701. <br />
  702. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  703. removed.
  704. </p>
  705. <pre>
  706. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  707. =&gt; [2, 3, 4]
  708. </pre>
  709. <p id="union">
  710. <b class="header">union</b><code>_.union(*arrays)</code>
  711. <br />
  712. Computes the union of the passed-in <b>arrays</b>: the list of unique items,
  713. in order, that are present in one or more of the <b>arrays</b>.
  714. </p>
  715. <pre>
  716. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  717. =&gt; [1, 2, 3, 101, 10]
  718. </pre>
  719. <p id="intersection">
  720. <b class="header">intersection</b><code>_.intersection(*arrays)</code>
  721. <br />
  722. Computes the list of values that are the intersection of all the <b>arrays</b>.
  723. Each value in the result is present in each of the <b>arrays</b>.
  724. </p>
  725. <pre>
  726. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  727. =&gt; [1, 2]
  728. </pre>
  729. <p id="difference">
  730. <b class="header">difference</b><code>_.difference(array, *others)</code>
  731. <br />
  732. Similar to <b>without</b>, but returns the values from <b>array</b> that
  733. are not present in the <b>other</b> arrays.
  734. </p>
  735. <pre>
  736. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  737. =&gt; [1, 3, 4]
  738. </pre>
  739. <p id="uniq">
  740. <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
  741. <span class="alias">Alias: <b>unique</b></span>
  742. <br />
  743. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  744. object equality. If you know in advance that the <b>array</b> is sorted,
  745. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  746. If you want to compute unique items based on a transformation, pass an
  747. <b>iterator</b> function.
  748. </p>
  749. <pre>
  750. _.uniq([1, 2, 1, 3, 1, 4]);
  751. =&gt; [1, 2, 3, 4]
  752. </pre>
  753. <p id="zip">
  754. <b class="header">zip</b><code>_.zip(*arrays)</code>
  755. <br />
  756. Merges together the values of each of the <b>arrays</b> with the
  757. values at the corresponding position. Useful when you have separate
  758. data sources that are coordinated through matching array indexes.
  759. If you're working with a matrix of nested arrays, <b>zip.apply</b>
  760. can transpose the matrix in a similar fashion.
  761. </p>
  762. <pre>
  763. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  764. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  765. </pre>
  766. <p id="object">
  767. <b class="header">object</b><code>_.object(list, [values])</code>
  768. <br />
  769. Converts arrays into objects. Pass either a single list of
  770. <tt>[key, value]</tt> pairs, or a list of keys, and a list of values.
  771. </p>
  772. <pre>
  773. _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
  774. =&gt; {moe: 30, larry: 40, curly: 50}
  775. _.object([['moe', 30], ['larry', 40], ['curly', 50]]);
  776. =&gt; {moe: 30, larry: 40, curly: 50}
  777. </pre>
  778. <p id="indexOf">
  779. <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
  780. <br />
  781. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  782. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  783. <b>indexOf</b> function unless it's missing. If you're working with a
  784. large array, and you know that the array is already sorted, pass <tt>true</tt>
  785. for <b>isSorted</b> to use a faster binary search ... or, pass a number as
  786. the third argument in order to look for the first matching value in the
  787. array after the given index.
  788. </p>
  789. <pre>
  790. _.indexOf([1, 2, 3], 2);
  791. =&gt; 1
  792. </pre>
  793. <p id="lastIndexOf">
  794. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value, [fromIndex])</code>
  795. <br />
  796. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  797. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  798. function if possible. Pass <b>fromIndex</b> to start your search at a
  799. given index.
  800. </p>
  801. <pre>
  802. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  803. =&gt; 4
  804. </pre>
  805. <p id="sortedIndex">
  806. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  807. <br />
  808. Uses a binary search to determine the index at which the <b>value</b>
  809. <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  810. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  811. the sort ranking of each value, including the <b>value</b> you pass.
  812. </p>
  813. <pre>
  814. _.sortedIndex([10, 20, 30, 40, 50], 35);
  815. =&gt; 3
  816. </pre>
  817. <p id="range">
  818. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  819. <br />
  820. A function to create flexibly-numbered lists of integers, handy for
  821. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  822. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  823. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  824. exclusive.
  825. </p>
  826. <pre>
  827. _.range(10);
  828. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  829. _.range(1, 11);
  830. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  831. _.range(0, 30, 5);
  832. =&gt; [0, 5, 10, 15, 20, 25]
  833. _.range(0, -10, -1);
  834. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  835. _.range(0);
  836. =&gt; []
  837. </pre>
  838. <h2 id="functions">Function (uh, ahem) Functions</h2>
  839. <p id="bind">
  840. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  841. <br />
  842. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  843. the function is called, the value of <i>this</i> will be the <b>object</b>.
  844. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  845. also known as <b>partial application</b>.
  846. </p>
  847. <pre>
  848. var func = function(greeting){ return greeting + ': ' + this.name };
  849. func = _.bind(func, {name : 'moe'}, 'hi');
  850. func();
  851. =&gt; 'hi: moe'
  852. </pre>
  853. <p id="bindAll">
  854. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  855. <br />
  856. Binds a number of methods on the <b>object</b>, specified by
  857. <b>methodNames</b>, to be run in the context of that object whenever they
  858. are invoked. Very handy for binding functions that are going to be used
  859. as event handlers, which would otherwise be invoked with a fairly useless
  860. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  861. function properties will be bound to it.
  862. </p>
  863. <pre>
  864. var buttonView = {
  865. label : 'underscore',
  866. onClick : function(){ alert('clicked: ' + this.label); },
  867. onHover : function(){ console.log('hovering: ' + this.label); }
  868. };
  869. _.bindAll(buttonView);
  870. jQuery('#underscore_button').bind('click', buttonView.onClick);
  871. =&gt; When the button is clicked, this.label will have the correct value...
  872. </pre>
  873. <p id="memoize">
  874. <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
  875. <br />
  876. Memoizes a given <b>function</b> by caching the computed result. Useful
  877. for speeding up slow-running computations. If passed an optional
  878. <b>hashFunction</b>, it will be used to compute the hash key for storing
  879. the result, based on the arguments to the original function. The default
  880. <b>hashFunction</b> just uses the first argument to the memoized function
  881. as the key.
  882. </p>
  883. <pre>
  884. var fibonacci = _.memoize(function(n) {
  885. return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  886. });
  887. </pre>
  888. <p id="delay">
  889. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  890. <br />
  891. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  892. milliseconds. If you pass the optional <b>arguments</b>, they will be
  893. forwarded on to the <b>function</b> when it is invoked.
  894. </p>
  895. <pre>
  896. var log = _.bind(console.log, console);
  897. _.delay(log, 1000, 'logged later');
  898. =&gt; 'logged later' // Appears after one second.
  899. </pre>
  900. <p id="defer">
  901. <b class="header">defer</b><code>_.defer(function, [*arguments])</code>
  902. <br />
  903. Defers invoking the <b>function</b> until the current call stack has cleared,
  904. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  905. expensive computations or HTML rendering in chunks without blocking the UI thread
  906. from updating. If you pass the optional <b>arguments</b>, they will be
  907. forwarded on to the <b>function</b> when it is invoked.
  908. </p>
  909. <pre>
  910. _.defer(function(){ alert('deferred'); });
  911. // Returns from the function before the alert runs.
  912. </pre>
  913. <p id="throttle">
  914. <b class="header">throttle</b><code>_.throttle(function, wait)</code>
  915. <br />
  916. Creates and returns a new, throttled version of the passed function,
  917. that, when invoked repeatedly, will only actually call the original function
  918. at most once per every <b>wait</b>
  919. milliseconds. Useful for rate-limiting events that occur faster than you
  920. can keep up with.
  921. </p>
  922. <pre>
  923. var throttled = _.throttle(updatePosition, 100);
  924. $(window).scroll(throttled);
  925. </pre>
  926. <p id="debounce">
  927. <b class="header">debounce</b><code>_.debounce(function, wait, [immediate])</code>
  928. <br />
  929. Creates and returns a new debounced version of the passed function that
  930. will postpone its execution until after
  931. <b>wait</b> milliseconds have elapsed since the last time it
  932. was invoked. Useful for implementing behavior that should only happen
  933. <i>after</i> the input has stopped arriving. For example: rendering a
  934. preview of a Markdown comment, recalculating a layout after the window
  935. has stopped being resized, and so on.
  936. </p>
  937. <p>
  938. Pass <tt>true</tt> for the <b>immediate</b> parameter to cause
  939. <b>debounce</b> to trigger the function on the leading instead of the
  940. trailing edge of the <b>wait</b> interval. Useful in circumstances like
  941. preventing accidental double-clicks on a "submit" button from firing a
  942. second time.
  943. </p>
  944. <pre>
  945. var lazyLayout = _.debounce(calculateLayout, 300);
  946. $(window).resize(lazyLayout);
  947. </pre>
  948. <p id="once">
  949. <b class="header">once</b><code>_.once(function)</code>
  950. <br />
  951. Creates a version of the function that can only be called one time.
  952. Repeated calls to the modified function will have no effect, returning
  953. the value from the original call. Useful for initialization functions,
  954. instead of having to set a boolean flag and then check it later.
  955. </p>
  956. <pre>
  957. var initialize = _.once(createApplication);
  958. initialize();
  959. initialize();
  960. // Application is only created once.
  961. </pre>
  962. <p id="after">
  963. <b class="header">after</b><code>_.after(count, function)</code>
  964. <br />
  965. Creates a version of the function that will only be run after first
  966. being called <b>count</b> times. Useful for grouping asynchronous responses,
  967. where you want to be sure that all the async calls have finished, before
  968. proceeding.
  969. </p>
  970. <pre>
  971. var renderNotes = _.after(notes.length, render);
  972. _.each(notes, function(note) {
  973. note.asyncSave({success: renderNotes});
  974. });
  975. // renderNotes is run once, after all notes have saved.
  976. </pre>
  977. <p id="wrap">
  978. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  979. <br />
  980. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  981. passing it as the first argument. This allows the <b>wrapper</b> to
  982. execute code before and after the <b>function</b> runs, adjust the arguments,
  983. and execute it conditionally.
  984. </p>
  985. <pre>
  986. var hello = function(name) { return "hello: " + name; };
  987. hello = _.wrap(hello, function(func) {
  988. return "before, " + func("moe") + ", after";
  989. });
  990. hello();
  991. =&gt; 'before, hello: moe, after'
  992. </pre>
  993. <p id="compose">
  994. <b class="header">compose</b><code>_.compose(*functions)</code>
  995. <br />
  996. Returns the composition of a list of <b>functions</b>, where each function
  997. consumes the return value of the function that follows. In math terms,
  998. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  999. <i>f(g(h()))</i>.
  1000. </p>
  1001. <pre>
  1002. var greet = function(name){ return "hi: " + name; };
  1003. var exclaim = function(statement){ return statement + "!"; };
  1004. var welcome = _.compose(exclaim, greet);
  1005. welcome('moe');
  1006. =&gt; 'hi: moe!'
  1007. </pre>
  1008. <h2 id="objects">Object Functions</h2>
  1009. <p id="keys">
  1010. <b class="header">keys</b><code>_.keys(object)</code>
  1011. <br />
  1012. Retrieve all the names of the <b>object</b>'s properties.
  1013. </p>
  1014. <pre>
  1015. _.keys({one : 1, two : 2, three : 3});
  1016. =&gt; ["one", "two", "three"]
  1017. </pre>
  1018. <p id="values">
  1019. <b class="header">values</b><code>_.values(object)</code>
  1020. <br />
  1021. Return all of the values of the <b>object</b>'s properties.
  1022. </p>
  1023. <pre>
  1024. _.values({one : 1, two : 2, three : 3});
  1025. =&gt; [1, 2, 3]
  1026. </pre>
  1027. <p id="pairs">
  1028. <b class="header">pairs</b><code>_.pairs(object)</code>
  1029. <br />
  1030. Convert an object into a list of <tt>[key, value]</tt> pairs.
  1031. </p>
  1032. <pre>
  1033. _.pairs({one: 1, two: 2, three: 3});
  1034. =&gt; [["one", 1], ["two", 2], ["three", 3]]
  1035. </pre>
  1036. <p id="invert">
  1037. <b class="header">invert</b><code>_.invert(object)</code>
  1038. <br />
  1039. Returns a copy of the <b>object</b> where the keys have become the values
  1040. and the values the keys. For this to work, all of your object's values
  1041. should be unique and string serializable.
  1042. </p>
  1043. <pre>
  1044. _.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"});
  1045. =&gt; {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};
  1046. </pre>
  1047. <p id="object-functions">
  1048. <b class="header">functions</b><code>_.functions(object)</code>
  1049. <span class="alias">Alias: <b>methods</b></span>
  1050. <br />
  1051. Returns a sorted list of the names of every method in an object &mdash;
  1052. that is to say, the name of every function property of the object.
  1053. </p>
  1054. <pre>
  1055. _.functions(_);
  1056. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  1057. </pre>
  1058. <p id="extend">
  1059. <b class="header">extend</b><code>_.extend(destination, *sources)</code>
  1060. <br />
  1061. Copy all of the properties in the <b>source</b> objects over to the
  1062. <b>destination</b> object, and return the <b>destination</b> object.
  1063. It's in-order, so the last source will override properties of the same
  1064. name in previous arguments.
  1065. </p>
  1066. <pre>
  1067. _.extend({name : 'moe'}, {age : 50});
  1068. =&gt; {name : 'moe', age : 50}
  1069. </pre>
  1070. <p id="pick">
  1071. <b class="header">pick</b><code>_.pick(object, *keys)</code>
  1072. <br />
  1073. Return a copy of the <b>object</b>, filtered to only have values for
  1074. the whitelisted <b>keys</b> (or array of valid keys).
  1075. </p>
  1076. <pre>
  1077. _.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
  1078. =&gt; {name : 'moe', age : 50}
  1079. </pre>
  1080. <p id="omit">
  1081. <b class="header">omit</b><code>_.omit(object, *keys)</code>
  1082. <br />
  1083. Return a copy of the <b>object</b>, filtered to omit the blacklisted
  1084. <b>keys</b> (or array of keys).
  1085. </p>
  1086. <pre>
  1087. _.omit({name : 'moe', age : 50, userid : 'moe1'}, 'userid');
  1088. =&gt; {name : 'moe', age : 50}
  1089. </pre>
  1090. <p id="defaults">
  1091. <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
  1092. <br />
  1093. Fill in null and undefined properties in <b>object</b> with values from the
  1094. <b>defaults</b> objects, and return the <b>object</b>. As soon as the
  1095. property is filled, further defaults will have no effect.
  1096. </p>
  1097. <pre>
  1098. var iceCream = {flavor : "chocolate"};
  1099. _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
  1100. =&gt; {flavor : "chocolate", sprinkles : "lots"}
  1101. </pre>
  1102. <p id="clone">
  1103. <b class="header">clone</b><code>_.clone(object)</code>
  1104. <br />
  1105. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  1106. or arrays will be copied by reference, not duplicated.
  1107. </p>
  1108. <pre>
  1109. _.clone({name : 'moe'});
  1110. =&gt; {name : 'moe'};
  1111. </pre>
  1112. <p id="tap">
  1113. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  1114. <br />
  1115. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  1116. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  1117. </p>
  1118. <pre>
  1119. _.chain([1,2,3,200])
  1120. .filter(function(num) { return num % 2 == 0; })
  1121. .tap(alert)
  1122. .map(function(num) { return num * num })
  1123. .value();
  1124. =&gt; // [2, 200] (alerted)
  1125. =&gt; [4, 40000]
  1126. </pre>
  1127. <p id="has">
  1128. <b class="header">has</b><code>_.has(object, key)</code>
  1129. <br />
  1130. Does the object contain the given key? Identical to
  1131. <tt>object.hasOwnProperty(key)</tt>, but uses a safe reference to the
  1132. <tt>hasOwnProperty</tt> function, in case it's been
  1133. <a href="http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/">overridden accidentally</a>.
  1134. </p>
  1135. <pre>
  1136. _.has({a: 1, b: 2, c: 3}, "b");
  1137. =&gt; true
  1138. </pre>
  1139. <p id="isEqual">
  1140. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  1141. <br />
  1142. Performs an optimized deep comparison between the two objects, to determine
  1143. if they should be considered equal.
  1144. </p>
  1145. <pre>
  1146. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  1147. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  1148. moe == clone;
  1149. =&gt; false
  1150. _.isEqual(moe, clone);
  1151. =&gt; true
  1152. </pre>
  1153. <p id="isEmpty">
  1154. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  1155. <br />
  1156. Returns <i>true</i> if <b>object</b> contains no values.
  1157. </p>
  1158. <pre>
  1159. _.isEmpty([1, 2, 3]);
  1160. =&gt; false
  1161. _.isEmpty({});
  1162. =&gt; true
  1163. </pre>
  1164. <p id="isElement">
  1165. <b class="header">isElement</b><code>_.isElement(object)</code>
  1166. <br />
  1167. Returns <i>true</i> if <b>object</b> is a DOM element.
  1168. </p>
  1169. <pre>
  1170. _.isElement(jQuery('body')[0]);
  1171. =&gt; true
  1172. </pre>
  1173. <p id="isArray">
  1174. <b class="header">isArray</b><code>_.isArray(object)</code>
  1175. <br />
  1176. Returns <i>true</i> if <b>object</b> is an Array.
  1177. </p>
  1178. <pre>
  1179. (function(){ return _.isArray(arguments); })();
  1180. =&gt; false
  1181. _.isArray([1,2,3]);
  1182. =&gt; true
  1183. </pre>
  1184. <p id="isObject">
  1185. <b class="header">isObject</b><code>_.isObject(value)</code>
  1186. <br />
  1187. Returns <i>true</i> if <b>value</b> is an Object. Note that JavaScript
  1188. arrays and functions are objects, while (normal) strings and numbers are not.
  1189. </p>
  1190. <pre>
  1191. _.isObject({});
  1192. =&gt; true
  1193. _.isObject(1);
  1194. =&gt; false
  1195. </pre>
  1196. <p id="isArguments">
  1197. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  1198. <br />
  1199. Returns <i>true</i> if <b>object</b> is an Arguments object.
  1200. </p>
  1201. <pre>
  1202. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  1203. =&gt; true
  1204. _.isArguments([1,2,3]);
  1205. =&gt; false
  1206. </pre>
  1207. <p id="isFunction">
  1208. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  1209. <br />
  1210. Returns <i>true</i> if <b>object</b> is a Function.
  1211. </p>
  1212. <pre>
  1213. _.isFunction(alert);
  1214. =&gt; true
  1215. </pre>
  1216. <p id="isString">
  1217. <b class="header">isString</b><code>_.isString(object)</code>
  1218. <br />
  1219. Returns <i>true</i> if <b>object</b> is a String.
  1220. </p>
  1221. <pre>
  1222. _.isString("moe");
  1223. =&gt; true
  1224. </pre>
  1225. <p id="isNumber">
  1226. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  1227. <br />
  1228. Returns <i>true</i> if <b>object</b> is a Number (including <tt>NaN</tt>).
  1229. </p>
  1230. <pre>
  1231. _.isNumber(8.4 * 5);
  1232. =&gt; true
  1233. </pre>
  1234. <p id="isFinite">
  1235. <b class="header">isFinite</b><code>_.isFinite(object)</code>
  1236. <br />
  1237. Returns <i>true</i> if <b>object</b> is a finite Number.
  1238. </p>
  1239. <pre>
  1240. _.isFinite(-101);
  1241. =&gt; true
  1242. _.isFinite(-Infinity);
  1243. =&gt; false
  1244. </pre>
  1245. <p id="isBoolean">
  1246. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  1247. <br />
  1248. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  1249. </p>
  1250. <pre>
  1251. _.isBoolean(null);
  1252. =&gt; false
  1253. </pre>
  1254. <p id="isDate">
  1255. <b class="header">isDate</b><code>_.isDate(object)</code>
  1256. <br />
  1257. Returns <i>true</i> if <b>object</b> is a Date.
  1258. </p>
  1259. <pre>
  1260. _.isDate(new Date());
  1261. =&gt; true
  1262. </pre>
  1263. <p id="isRegExp">
  1264. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  1265. <br />
  1266. Returns <i>true</i> if <b>object</b> is a RegExp.
  1267. </p>
  1268. <pre>
  1269. _.isRegExp(/moe/);
  1270. =&gt; true
  1271. </pre>
  1272. <p id="isNaN">
  1273. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  1274. <br />
  1275. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  1276. the same as the native <b>isNaN</b> function, which will also return
  1277. true if the variable is <i>undefined</i>.
  1278. </p>
  1279. <pre>
  1280. _.isNaN(NaN);
  1281. =&gt; true
  1282. isNaN(undefined);
  1283. =&gt; true
  1284. _.isNaN(undefined);
  1285. =&gt; false
  1286. </pre>
  1287. <p id="isNull">
  1288. <b class="header">isNull</b><code>_.isNull(object)</code>
  1289. <br />
  1290. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  1291. </p>
  1292. <pre>
  1293. _.isNull(null);
  1294. =&gt; true
  1295. _.isNull(undefined);
  1296. =&gt; false
  1297. </pre>
  1298. <p id="isUndefined">
  1299. <b class="header">isUndefined</b><code>_.isUndefined(value)</code>
  1300. <br />
  1301. Returns <i>true</i> if <b>value</b> is <i>undefined</i>.
  1302. </p>
  1303. <pre>
  1304. _.isUndefined(window.missingVariable);
  1305. =&gt; true
  1306. </pre>
  1307. <h2 id="utility">Utility Functions</h2>
  1308. <p id="noConflict">
  1309. <b class="header">noConflict</b><code>_.noConflict()</code>
  1310. <br />
  1311. Give control of the "_" variable back to its previous owner. Returns
  1312. a reference to the <b>Underscore</b> object.
  1313. </p>
  1314. <pre>
  1315. var underscore = _.noConflict();</pre>
  1316. <p id="identity">
  1317. <b class="header">identity</b><code>_.identity(value)</code>
  1318. <br />
  1319. Returns the same value that is used as the argument. In math:
  1320. <tt>f(x) = x</tt><br />
  1321. This function looks useless, but is used throughout Underscore as
  1322. a default iterator.
  1323. </p>
  1324. <pre>
  1325. var moe = {name : 'moe'};
  1326. moe === _.identity(moe);
  1327. =&gt; true</pre>
  1328. <p id="times">
  1329. <b class="header">times</b><code>_.times(n, iterator, [context])</code>
  1330. <br />
  1331. Invokes the given iterator function <b>n</b> times. Each invocation of
  1332. <b>iterator</b> is called with an <tt>index</tt> argument.
  1333. <br />
  1334. <i>Note: this example uses the <a href="#chaining">chaining syntax</a></i>.
  1335. </p>
  1336. <pre>
  1337. _(3).times(function(n){ genie.grantWishNumber(n); });</pre>
  1338. <p id="random">
  1339. <b class="header">random</b><code>_.random(min, max)</code>
  1340. <br />
  1341. Returns a random integer between <b>min</b> and <b>max</b>, inclusive.
  1342. If you only pass one argument, it will return a number between <tt>0</tt>
  1343. and that number.
  1344. </p>
  1345. <pre>
  1346. _.random(0, 100);
  1347. =&gt; 42</pre>
  1348. <p id="mixin">
  1349. <b class="header">mixin</b><code>_.mixin(object)</code>
  1350. <br />
  1351. Allows you to extend Underscore with your own utility functions. Pass
  1352. a hash of <tt>{name: function}</tt> definitions to have your functions
  1353. added to the Underscore object, as well as the OOP wrapper.
  1354. </p>
  1355. <pre>
  1356. _.mixin({
  1357. capitalize : function(string) {
  1358. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  1359. }
  1360. });
  1361. _("fabio").capitalize();
  1362. =&gt; "Fabio"
  1363. </pre>
  1364. <p id="uniqueId">
  1365. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  1366. <br />
  1367. Generate a globally-unique id for client-side models or DOM elements
  1368. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  1369. Without <b>prefix</b>, returns an integer.
  1370. </

Large files files are truncated, but you can click here to view the full file