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>
11 background: #f4f4f4 url(docs/images/background.png);
13 font-family: Helvetica Neue, Helvetica, Arial;
16 font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
21 top: 0; left: 0; bottom: 0;
25 padding: 15px 0 30px 30px;
26 border-right: 1px solid #bbb;
27 box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
29 a.toc_title, a.toc_title:visited {
36 text-decoration: underline;
47 list-style-type: none;
48 font-family: Lucida Grande;
55 text-decoration: none;
58 .toc_section li a:hover {
59 text-decoration: underline;
63 margin: 40px 0 50px 260px;
75 h1, h2, h3, h4, h5, h6 {
91 margin: 0; padding: 0;
94 padding: 2px 12px 2px 0;
97 list-style-type: circle;
105 font-family: Monaco, Consolas, "Lucida Console", monospace;
113 border: 1px solid #ddd;
121 padding: 2px 0 2px 15px;
122 border-left: 5px solid #bbb;
129 <div id="sidebar" class="interface">
131 <a class="toc_title" href="#">
132 Underscore.js <span class="version">(1.2.4)</span>
135 <a class="toc_title" href="#">
139 <a class="toc_title" href="#collections">
142 <ul class="toc_section">
143 <li>- <a href="#each">each</a></li>
144 <li>- <a href="#map">map</a></li>
145 <li>- <a href="#reduce">reduce</a></li>
146 <li>- <a href="#reduceRight">reduceRight</a></li>
147 <li>- <a href="#find">find</a></li>
148 <li>- <a href="#filter">filter</a></li>
149 <li>- <a href="#reject">reject</a></li>
150 <li>- <a href="#all">all</a></li>
151 <li>- <a href="#any">any</a></li>
152 <li>- <a href="#include">include</a></li>
153 <li>- <a href="#invoke">invoke</a></li>
154 <li>- <a href="#pluck">pluck</a></li>
155 <li>- <a href="#max">max</a></li>
156 <li>- <a href="#min">min</a></li>
157 <li>- <a href="#sortBy">sortBy</a></li>
158 <li>- <a href="#groupBy">groupBy</a></li>
159 <li>- <a href="#sortedIndex">sortedIndex</a></li>
160 <li>- <a href="#shuffle">shuffle</a></li>
161 <li>- <a href="#toArray">toArray</a></li>
162 <li>- <a href="#size">size</a></li>
165 <a class="toc_title" href="#arrays">
168 <ul class="toc_section">
169 <li>- <a href="#first">first</a></li>
170 <li>- <a href="#initial">initial</a></li>
171 <li>- <a href="#last">last</a></li>
172 <li>- <a href="#rest">rest</a></li>
173 <li>- <a href="#compact">compact</a></li>
174 <li>- <a href="#flatten">flatten</a></li>
175 <li>- <a href="#without">without</a></li>
176 <li>- <a href="#union">union</a></li>
177 <li>- <a href="#intersection">intersection</a></li>
178 <li>- <a href="#difference">difference</a></li>
179 <li>- <a href="#uniq">uniq</a></li>
180 <li>- <a href="#zip">zip</a></li>
181 <li>- <a href="#indexOf">indexOf</a></li>
182 <li>- <a href="#lastIndexOf">lastIndexOf</a></li>
183 <li>- <a href="#range">range</a></li>
186 <a class="toc_title" href="#functions">
189 <ul class="toc_section">
190 <li>- <a href="#bind">bind</a></li>
191 <li>- <a href="#bindAll">bindAll</a></li>
192 <li>- <a href="#memoize">memoize</a></li>
193 <li>- <a href="#delay">delay</a></li>
194 <li>- <a href="#defer">defer</a></li>
195 <li>- <a href="#throttle">throttle</a></li>
196 <li>- <a href="#debounce">debounce</a></li>
197 <li>- <a href="#once">once</a></li>
198 <li>- <a href="#after">after</a></li>
199 <li>- <a href="#wrap">wrap</a></li>
200 <li>- <a href="#compose">compose</a></li>
203 <a class="toc_title" href="#objects">
206 <ul class="toc_section">
207 <li>- <a href="#keys">keys</a></li>
208 <li>- <a href="#values">values</a></li>
209 <li>- <a href="#functions">functions</a></li>
210 <li>- <a href="#extend">extend</a></li>
211 <li>- <a href="#defaults">defaults</a></li>
212 <li>- <a href="#clone">clone</a></li>
213 <li>- <a href="#tap">tap</a></li>
214 <li>- <a href="#isEqual">isEqual</a></li>
215 <li>- <a href="#isEmpty">isEmpty</a></li>
216 <li>- <a href="#isElement">isElement</a></li>
217 <li>- <a href="#isArray">isArray</a></li>
218 <li>- <a href="#isArguments">isArguments</a></li>
219 <li>- <a href="#isFunction">isFunction</a></li>
220 <li>- <a href="#isString">isString</a></li>
221 <li>- <a href="#isNumber">isNumber</a></li>
222 <li>- <a href="#isBoolean">isBoolean</a></li>
223 <li>- <a href="#isDate">isDate</a></li>
224 <li>- <a href="#isRegExp">isRegExp</a></li>
225 <li>- <a href="#isNaN">isNaN</a></li>
226 <li>- <a href="#isNull">isNull</a></li>
227 <li>- <a href="#isUndefined">isUndefined</a></li>
230 <a class="toc_title" href="#utility">
233 <ul class="toc_section">
234 <li>- <a href="#noConflict">noConflict</a></li>
235 <li>- <a href="#identity">identity</a></li>
236 <li>- <a href="#times">times</a></li>
237 <li>- <a href="#mixin">mixin</a></li>
238 <li>- <a href="#uniqueId">uniqueId</a></li>
239 <li>- <a href="#escape">escape</a></li>
240 <li>- <a href="#template">template</a></li>
243 <a class="toc_title" href="#chaining">
246 <ul class="toc_section">
247 <li>- <a href="#chain">chain</a></li>
248 <li>- <a href="#value">value</a></li>
251 <a class="toc_title" href="#links">
255 <a class="toc_title" href="#changelog">
261 <div class="container">
263 <p id="introduction">
264 <img style="width: 396px; height: 69px;" src="docs/images/underscore.png" alt="Underscore.js" />
268 <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
269 utility-belt library for JavaScript that provides a lot of the
270 functional programming support that you would expect in
271 <a href="http://prototypejs.org/api">Prototype.js</a>
272 (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
273 but without extending any of the built-in JavaScript objects. It's the
274 tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
275 and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
279 Underscore provides 60-odd functions that support both the usual
280 functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> —
281 as well as more specialized helpers: function binding, javascript
282 templating, deep equality testing, and so on. It delegates to built-in
283 functions, if present, so modern browsers will use the
284 native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
285 <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
289 A complete <a href="test/test.html">Test & Benchmark Suite</a>
290 is included for your perusal.
294 You may also read through the <a href="docs/underscore.html">annotated source code</a>.
299 <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
300 You can report bugs and discuss features on the
301 <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
302 on Freenode in the <tt>#documentcloud</tt> channel,
303 or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
307 <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
310 <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
314 <td><a href="underscore.js">Development Version (1.2.4)</a></td>
315 <td><i>34kb, Uncompressed with Comments</i></td>
318 <td><a href="underscore-min.js">Production Version (1.2.4)</a></td>
319 <td><i>< 4kb, Minified and Gzipped</i></td>
323 <div id="documentation">
325 <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
328 <b class="header">each</b><code>_.each(list, iterator, [context])</code>
329 <span class="alias">Alias: <b>forEach</b></span>
331 Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
332 function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
333 passed. Each invocation of <b>iterator</b> is called with three arguments:
334 <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
335 arguments will be <tt>(value, key, list)</tt>. Delegates to the native
336 <b>forEach</b> function if it exists.
339 _.each([1, 2, 3], function(num){ alert(num); });
340 => alerts each number in turn...
341 _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
342 => alerts each number in turn...</pre>
345 <b class="header">map</b><code>_.map(list, iterator, [context])</code>
347 Produces a new array of values by mapping each value in <b>list</b>
348 through a transformation function (<b>iterator</b>). If the native <b>map</b> method
349 exists, it will be used instead. If <b>list</b> is a JavaScript object,
350 <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
353 _.map([1, 2, 3], function(num){ return num * 3; });
355 _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
356 => [3, 6, 9]</pre>
359 <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
360 <span class="alias">Aliases: <b>inject, foldl</b></span>
362 Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
363 <b>list</b> of values into a single value. <b>Memo</b> is the initial state
364 of the reduction, and each successive step of it should be returned by
368 var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
373 <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
374 <span class="alias">Alias: <b>foldr</b></span>
376 The right-associative version of <b>reduce</b>. Delegates to the
377 JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
378 is not as useful in JavaScript as it would be in a language with lazy
382 var list = [[0, 1], [2, 3], [4, 5]];
383 var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
384 => [4, 5, 2, 3, 0, 1]
388 <b class="header">find</b><code>_.find(list, iterator, [context])</code>
389 <span class="alias">Alias: <b>detect</b></span>
391 Looks through each value in the <b>list</b>, returning the first one that
392 passes a truth test (<b>iterator</b>). The function returns as
393 soon as it finds an acceptable element, and doesn't traverse the
397 var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
402 <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
403 <span class="alias">Alias: <b>select</b></span>
405 Looks through each value in the <b>list</b>, returning an array of all
406 the values that pass a truth test (<b>iterator</b>). Delegates to the
407 native <b>filter</b> method, if it exists.
410 var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
415 <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
417 Returns the values in <b>list</b> without the elements that the truth
418 test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
421 var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
426 <b class="header">all</b><code>_.all(list, iterator, [context])</code>
427 <span class="alias">Alias: <b>every</b></span>
429 Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
430 truth test. Delegates to the native method <b>every</b>, if present.
433 _.all([true, 1, null, 'yes'], _.identity);
438 <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
439 <span class="alias">Alias: <b>some</b></span>
441 Returns <i>true</i> if any of the values in the <b>list</b> pass the
442 <b>iterator</b> truth test. Short-circuits and stops traversing the list
443 if a true element is found. Delegates to the native method <b>some</b>,
447 _.any([null, 0, 'yes', false]);
452 <b class="header">include</b><code>_.include(list, value)</code>
453 <span class="alias">Alias: <b>contains</b></span>
455 Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
456 <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
460 _.include([1, 2, 3], 3);
465 <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
467 Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
468 Any extra arguments passed to <b>invoke</b> will be forwarded on to the
472 _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
473 => [[1, 5, 7], [1, 2, 3]]
477 <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
479 A convenient version of what is perhaps the most common use-case for
480 <b>map</b>: extracting a list of property values.
483 var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
484 _.pluck(stooges, 'name');
485 => ["moe", "larry", "curly"]
489 <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
491 Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
492 it will be used on each value to generate the criterion by which the
496 var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
497 _.max(stooges, function(stooge){ return stooge.age; });
498 => {name : 'curly', age : 60};
502 <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
504 Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
505 it will be used on each value to generate the criterion by which the
509 var numbers = [10, 5, 100, 2, 1000];
515 <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
517 Returns a sorted copy of <b>list</b>, ranked by the results of running
518 each value through <b>iterator</b>.
521 _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
522 => [5, 4, 6, 3, 1, 2]
526 <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
528 Splits a collection into sets, grouped by the result of running each
529 value through <b>iterator</b>. If <b>iterator</b> is a string instead of
530 a function, groups by the property named by <b>iterator</b> on each of
534 _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
535 => {1: [1.3], 2: [2.1, 2.4]}
537 _.groupBy(['one', 'two', 'three'], 'length');
538 => {3: ["one", "two"], 5: ["three"]}
542 <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
544 Uses a binary search to determine the index at which the <b>value</b>
545 <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
546 sorted order. If an <b>iterator</b> is passed, it will be used to compute
547 the sort ranking of each value.
550 _.sortedIndex([10, 20, 30, 40, 50], 35);
555 <b class="header">shuffle</b><code>_.shuffle(list)</code>
557 Returns a shuffled copy of the <b>list</b>, using a version of the
558 <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
561 _.shuffle([1, 2, 3, 4, 5, 6]);
562 => [4, 1, 6, 3, 5, 2]
566 <b class="header">toArray</b><code>_.toArray(list)</code>
568 Converts the <b>list</b> (anything that can be iterated over), into a
569 real Array. Useful for transmuting the <b>arguments</b> object.
572 (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
577 <b class="header">size</b><code>_.size(list)</code>
579 Return the number of values in the <b>list</b>.
582 _.size({one : 1, two : 2, three : 3});
586 <h2 id="arrays">Array Functions</h2>
589 <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
593 <b class="header">first</b><code>_.first(array, [n])</code>
594 <span class="alias">Alias: <b>head</b></span>
596 Returns the first element of an <b>array</b>. Passing <b>n</b> will
597 return the first <b>n</b> elements of the array.
600 _.first([5, 4, 3, 2, 1]);
605 <b class="header">initial</b><code>_.initial(array, [n])</code>
607 Returns everything but the last entry of the array. Especially useful on
608 the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
612 _.initial([5, 4, 3, 2, 1]);
617 <b class="header">last</b><code>_.last(array, [n])</code>
619 Returns the last element of an <b>array</b>. Passing <b>n</b> will return
620 the last <b>n</b> elements of the array.
623 _.last([5, 4, 3, 2, 1]);
628 <b class="header">rest</b><code>_.rest(array, [index])</code>
629 <span class="alias">Alias: <b>tail</b></span>
631 Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
632 to return the values of the array from that index onward.
635 _.rest([5, 4, 3, 2, 1]);
640 <b class="header">compact</b><code>_.compact(array)</code>
642 Returns a copy of the <b>array</b> with all falsy values removed.
643 In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
644 <i>undefined</i> and <i>NaN</i> are all falsy.
647 _.compact([0, 1, false, 2, '', 3]);
652 <b class="header">flatten</b><code>_.flatten(array)</code>
654 Flattens a nested <b>array</b> (the nesting can be to any depth).
657 _.flatten([1, [2], [3, [[[4]]]]]);
662 <b class="header">without</b><code>_.without(array, [*values])</code>
664 Returns a copy of the <b>array</b> with all instances of the <b>values</b>
665 removed. <i>===</i> is used for the equality test.
668 _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
673 <b class="header">union</b><code>_.union(*arrays)</code>
675 Computes the union of the passed-in <b>arrays</b>: the list of unique items,
676 in order, that are present in one or more of the <b>arrays</b>.
679 _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
680 => [1, 2, 3, 101, 10]
683 <p id="intersection">
684 <b class="header">intersection</b><code>_.intersection(*arrays)</code>
686 Computes the list of values that are the intersection of all the <b>arrays</b>.
687 Each value in the result is present in each of the <b>arrays</b>.
690 _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
695 <b class="header">difference</b><code>_.difference(array, *others)</code>
697 Similar to <b>without</b>, but returns the values from <b>array</b> that
698 are not present in the <b>other</b> arrays.
701 _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
706 <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
707 <span class="alias">Alias: <b>unique</b></span>
709 Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
710 object equality. If you know in advance that the <b>array</b> is sorted,
711 passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
712 If you want to compute unique items based on a transformation, pass an
713 <b>iterator</b> function.
716 _.uniq([1, 2, 1, 3, 1, 4]);
721 <b class="header">zip</b><code>_.zip(*arrays)</code>
723 Merges together the values of each of the <b>arrays</b> with the
724 values at the corresponding position. Useful when you have separate
725 data sources that are coordinated through matching array indexes.
726 If you're working with a matrix of nested arrays, <b>zip.apply</b>
727 can transpose the matrix in a similar fashion.
730 _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
731 => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
735 <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
737 Returns the index at which <b>value</b> can be found in the <b>array</b>,
738 or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
739 <b>indexOf</b> function unless it's missing. If you're working with a
740 large array, and you know that the array is already sorted, pass <tt>true</tt>
741 for <b>isSorted</b> to use a faster binary search.
744 _.indexOf([1, 2, 3], 2);
749 <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
751 Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
752 or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
753 function if possible.
756 _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
761 <b class="header">range</b><code>_.range([start], stop, [step])</code>
763 A function to create flexibly-numbered lists of integers, handy for
764 <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
765 to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
766 from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
771 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
773 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
775 => [0, 5, 10, 15, 20, 25]
777 => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
782 <h2 id="functions">Function (uh, ahem) Functions</h2>
785 <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
787 Bind a <b>function</b> to an <b>object</b>, meaning that whenever
788 the function is called, the value of <i>this</i> will be the <b>object</b>.
789 Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
790 also known as <b>partial application</b>.
793 var func = function(greeting){ return greeting + ': ' + this.name };
794 func = _.bind(func, {name : 'moe'}, 'hi');
800 <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
802 Binds a number of methods on the <b>object</b>, specified by
803 <b>methodNames</b>, to be run in the context of that object whenever they
804 are invoked. Very handy for binding functions that are going to be used
805 as event handlers, which would otherwise be invoked with a fairly useless
806 <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
807 function properties will be bound to it.
811 label : 'underscore',
812 onClick : function(){ alert('clicked: ' + this.label); },
813 onHover : function(){ console.log('hovering: ' + this.label); }
815 _.bindAll(buttonView);
816 jQuery('#underscore_button').bind('click', buttonView.onClick);
817 => When the button is clicked, this.label will have the correct value...
821 <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
823 Memoizes a given <b>function</b> by caching the computed result. Useful
824 for speeding up slow-running computations. If passed an optional
825 <b>hashFunction</b>, it will be used to compute the hash key for storing
826 the result, based on the arguments to the original function. The default
827 <b>hashFunction</b> just uses the first argument to the memoized function
831 var fibonacci = _.memoize(function(n) {
832 return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
837 <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
839 Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
840 milliseconds. If you pass the optional <b>arguments</b>, they will be
841 forwarded on to the <b>function</b> when it is invoked.
844 var log = _.bind(console.log, console);
845 _.delay(log, 1000, 'logged later');
846 => 'logged later' // Appears after one second.
850 <b class="header">defer</b><code>_.defer(function)</code>
852 Defers invoking the <b>function</b> until the current call stack has cleared,
853 similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
854 expensive computations or HTML rendering in chunks without blocking the UI thread
858 _.defer(function(){ alert('deferred'); });
859 // Returns from the function before the alert runs.
863 <b class="header">throttle</b><code>_.throttle(function, wait)</code>
865 Creates and returns a new, throttled version of the passed function,
866 that, when invoked repeatedly, will only actually call the original function
867 at most once per every <b>wait</b>
868 milliseconds. Useful for rate-limiting events that occur faster than you
872 var throttled = _.throttle(updatePosition, 100);
873 $(window).scroll(throttled);
877 <b class="header">debounce</b><code>_.debounce(function, wait)</code>
879 Creates and returns a new debounced version of the passed function that
880 will postpone its execution until after
881 <b>wait</b> milliseconds have elapsed since the last time it
882 was invoked. Useful for implementing behavior that should only happen
883 <i>after</i> the input has stopped arriving. For example: rendering a
884 preview of a Markdown comment, recalculating a layout after the window
885 has stopped being resized, and so on.
888 var lazyLayout = _.debounce(calculateLayout, 300);
889 $(window).resize(lazyLayout);
893 <b class="header">once</b><code>_.once(function)</code>
895 Creates a version of the function that can only be called one time.
896 Repeated calls to the modified function will have no effect, returning
897 the value from the original call. Useful for initialization functions,
898 instead of having to set a boolean flag and then check it later.
901 var initialize = _.once(createApplication);
904 // Application is only created once.
908 <b class="header">after</b><code>_.after(count, function)</code>
910 Creates a version of the function that will only be run after first
911 being called <b>count</b> times. Useful for grouping asynchronous responses,
912 where you want to be sure that all the async calls have finished, before
916 var renderNotes = _.after(notes.length, render);
917 _.each(notes, function(note) {
918 note.asyncSave({success: renderNotes});
920 // renderNotes is run once, after all notes have saved.
924 <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
926 Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
927 passing it as the first argument. This allows the <b>wrapper</b> to
928 execute code before and after the <b>function</b> runs, adjust the arguments,
929 and execute it conditionally.
932 var hello = function(name) { return "hello: " + name; };
933 hello = _.wrap(hello, function(func) {
934 return "before, " + func("moe") + ", after";
937 => 'before, hello: moe, after'
941 <b class="header">compose</b><code>_.compose(*functions)</code>
943 Returns the composition of a list of <b>functions</b>, where each function
944 consumes the return value of the function that follows. In math terms,
945 composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
949 var greet = function(name){ return "hi: " + name; };
950 var exclaim = function(statement){ return statement + "!"; };
951 var welcome = _.compose(exclaim, greet);
956 <h2 id="objects">Object Functions</h2>
959 <b class="header">keys</b><code>_.keys(object)</code>
961 Retrieve all the names of the <b>object</b>'s properties.
964 _.keys({one : 1, two : 2, three : 3});
965 => ["one", "two", "three"]
969 <b class="header">values</b><code>_.values(object)</code>
971 Return all of the values of the <b>object</b>'s properties.
974 _.values({one : 1, two : 2, three : 3});
979 <b class="header">functions</b><code>_.functions(object)</code>
980 <span class="alias">Alias: <b>methods</b></span>
982 Returns a sorted list of the names of every method in an object —
983 that is to say, the name of every function property of the object.
987 => ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
991 <b class="header">extend</b><code>_.extend(destination, *sources)</code>
993 Copy all of the properties in the <b>source</b> objects over to the
994 <b>destination</b> object. It's in-order, so the last source will override
995 properties of the same name in previous arguments.
998 _.extend({name : 'moe'}, {age : 50});
999 => {name : 'moe', age : 50}
1003 <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
1005 Fill in missing properties in <b>object</b> with default values from the
1006 <b>defaults</b> objects. As soon as the property is filled, further defaults
1007 will have no effect.
1010 var iceCream = {flavor : "chocolate"};
1011 _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
1012 => {flavor : "chocolate", sprinkles : "lots"}
1016 <b class="header">clone</b><code>_.clone(object)</code>
1018 Create a shallow-copied clone of the <b>object</b>. Any nested objects
1019 or arrays will be copied by reference, not duplicated.
1022 _.clone({name : 'moe'});
1023 => {name : 'moe'};
1027 <b class="header">tap</b><code>_.tap(object, interceptor)</code>
1029 Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
1030 The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
1033 _.chain([1,2,3,200])
1034 .filter(function(num) { return num % 2 == 0; })
1036 .map(function(num) { return num * num })
1043 <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
1045 Performs an optimized deep comparison between the two objects, to determine
1046 if they should be considered equal.
1049 var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
1050 var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
1053 _.isEqual(moe, clone);
1058 <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
1060 Returns <i>true</i> if <b>object</b> contains no values.
1063 _.isEmpty([1, 2, 3]);
1070 <b class="header">isElement</b><code>_.isElement(object)</code>
1072 Returns <i>true</i> if <b>object</b> is a DOM element.
1075 _.isElement(jQuery('body')[0]);
1080 <b class="header">isArray</b><code>_.isArray(object)</code>
1082 Returns <i>true</i> if <b>object</b> is an Array.
1085 (function(){ return _.isArray(arguments); })();
1091 <p id="isArguments">
1092 <b class="header">isArguments</b><code>_.isArguments(object)</code>
1094 Returns <i>true</i> if <b>object</b> is an Arguments object.
1097 (function(){ return _.isArguments(arguments); })(1, 2, 3);
1099 _.isArguments([1,2,3]);
1104 <b class="header">isFunction</b><code>_.isFunction(object)</code>
1106 Returns <i>true</i> if <b>object</b> is a Function.
1109 _.isFunction(alert);
1114 <b class="header">isString</b><code>_.isString(object)</code>
1116 Returns <i>true</i> if <b>object</b> is a String.
1124 <b class="header">isNumber</b><code>_.isNumber(object)</code>
1126 Returns <i>true</i> if <b>object</b> is a Number (including <tt>NaN</tt>).
1129 _.isNumber(8.4 * 5);
1134 <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
1136 Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
1144 <b class="header">isDate</b><code>_.isDate(object)</code>
1146 Returns <i>true</i> if <b>object</b> is a Date.
1149 _.isDate(new Date());
1154 <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
1156 Returns <i>true</i> if <b>object</b> is a RegExp.
1164 <b class="header">isNaN</b><code>_.isNaN(object)</code>
1166 Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
1167 the same as the native <b>isNaN</b> function, which will also return
1168 true if the variable is <i>undefined</i>.
1180 <b class="header">isNull</b><code>_.isNull(object)</code>
1182 Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
1187 _.isNull(undefined);
1191 <p id="isUndefined">
1192 <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
1194 Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
1197 _.isUndefined(window.missingVariable);
1201 <h2 id="utility">Utility Functions</h2>
1204 <b class="header">noConflict</b><code>_.noConflict()</code>
1206 Give control of the "_" variable back to its previous owner. Returns
1207 a reference to the <b>Underscore</b> object.
1210 var underscore = _.noConflict();</pre>
1213 <b class="header">identity</b><code>_.identity(value)</code>
1215 Returns the same value that is used as the argument. In math:
1216 <tt>f(x) = x</tt><br />
1217 This function looks useless, but is used throughout Underscore as
1221 var moe = {name : 'moe'};
1222 moe === _.identity(moe);
1226 <b class="header">times</b><code>_.times(n, iterator)</code>
1228 Invokes the given iterator function <b>n</b> times.
1231 _(3).times(function(){ genie.grantWish(); });</pre>
1234 <b class="header">mixin</b><code>_.mixin(object)</code>
1236 Allows you to extend Underscore with your own utility functions. Pass
1237 a hash of <tt>{name: function}</tt> definitions to have your functions
1238 added to the Underscore object, as well as the OOP wrapper.
1242 capitalize : function(string) {
1243 return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
1246 _("fabio").capitalize();
1251 <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
1253 Generate a globally-unique id for client-side models or DOM elements
1254 that need one. If <b>prefix</b> is passed, the id will be appended to it.
1257 _.uniqueId('contact_');
1258 => 'contact_104'</pre>
1261 <b class="header">escape</b><code>_.escape(string)</code>
1263 Escapes a string for insertion into HTML, replacing
1264 <tt>&</tt>, <tt><</tt>, <tt>></tt>, <tt>"</tt>, <tt>'</tt>, and <tt>/</tt> characters.
1267 _.escape('Curly, Larry & Moe');
1268 => "Curly, Larry &amp; Moe"</pre>
1271 <b class="header">template</b><code>_.template(templateString, [context])</code>
1273 Compiles JavaScript templates into functions that can be evaluated
1274 for rendering. Useful for rendering complicated bits of HTML from JSON
1275 data sources. Template functions can both interpolate variables, using<br />
1276 <tt><%= … %></tt>, as well as execute arbitrary JavaScript code, with
1277 <tt><% … %></tt>. If you wish to interpolate a value, and have
1278 it be HTML-escaped, use <tt><%- … %></tt> When you evaluate a template function, pass in a
1279 <b>context</b> object that has properties corresponding to the template's free
1280 variables. If you're writing a one-off, you can pass the <b>context</b>
1281 object as the second parameter to <b>template</b> in order to render
1282 immediately instead of returning a template function.
1285 var compiled = _.template("hello: <%= name %>");
1286 compiled({name : 'moe'});
1289 var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
1290 _.template(list, {people : ['moe', 'curly', 'larry']});
1291 => "<li>moe</li><li>curly</li><li>larry</li>"
1293 var template = _.template("<b><%- value %></b>");
1294 template({value : '<script>'});
1295 => "<b>&lt;script&gt;</b>"</pre>
1298 You can also use <tt>print</tt> from within JavaScript code. This is
1299 sometimes more convenient than using <tt><%= ... %></tt>.
1303 var compiled = _.template("<% print('Hello ' + epithet); %>");
1304 compiled({epithet: "stooge"});
1305 => "Hello stooge."</pre>
1308 If ERB-style delimiters aren't your cup of tea, you can change Underscore's
1309 template settings to use different symbols to set off interpolated code.
1310 Define an <b>interpolate</b> regex to match expressions that should be
1311 interpolated verbatim, an <b>escape</b> regex to match expressions that should
1312 be inserted after being HTML escaped, and an <b>evaluate</b> regex to match
1313 expressions that should be evaluated without insertion into the resulting
1314 string. You may define or omit any combination of the three.
1315 For example, to perform
1316 <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
1321 _.templateSettings = {
1322 interpolate : /\{\{(.+?)\}\}/g
1325 var template = _.template("Hello {{ name }}!");
1326 template({name : "Mustache"});
1327 => "Hello Mustache!"</pre>
1330 <h2 id="chaining">Chaining</h2>
1333 You can use Underscore in either an object-oriented or a functional style,
1334 depending on your preference. The following two lines of code are
1335 identical ways to double a list of numbers.
1339 _.map([1, 2, 3], function(n){ return n * 2; });
1340 _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
1343 Using the object-oriented style allows you to chain together methods. Calling
1344 <tt>chain</tt> on a wrapped object will cause all future method calls to
1345 return wrapped objects as well. When you've finished the computation,
1346 use <tt>value</tt> to retrieve the final value. Here's an example of chaining
1347 together a <b>map/flatten/reduce</b>, in order to get the word count of
1348 every word in a song.
1353 {line : 1, words : "I'm a lumberjack and I'm okay"},
1354 {line : 2, words : "I sleep all night and I work all day"},
1355 {line : 3, words : "He's a lumberjack and he's okay"},
1356 {line : 4, words : "He sleeps all night and he works all day"}
1360 .map(function(line) { return line.words.split(' '); })
1362 .reduce(function(counts, word) {
1363 counts[word] = (counts[word] || 0) + 1;
1367 => {lumberjack : 2, all : 4, night : 2 ... }</pre>
1371 <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/prototype">Array prototype's methods</a>
1372 are proxied through the chained Underscore object, so you can slip a
1373 <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
1378 <b class="header">chain</b><code>_.chain(obj)</code>
1380 Returns a wrapped object. Calling methods on this object will continue
1381 to return wrapped objects until <tt>value</tt> is used.
1384 var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
1385 var youngest = _.chain(stooges)
1386 .sortBy(function(stooge){ return stooge.age; })
1387 .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
1394 <b class="header">value</b><code>_(obj).value()</code>
1396 Extracts the value of a wrapped object.
1399 _([1, 2, 3]).value();
1403 <h2 id="links">Links & Suggested Reading</h2>
1406 <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
1407 a Lua port of the functions that are applicable in both languages.
1408 Includes OOP-wrapping and chaining.
1409 The <a href="http://github.com/mirven/underscore.lua">source</a> is
1410 available on GitHub.
1414 <a href="http://brianhaveri.github.com/Underscore.php/">Underscore.php</a>,
1415 a PHP port of the functions that are applicable in both languages.
1416 Includes OOP-wrapping and chaining.
1417 The <a href="http://github.com/brianhaveri/Underscore.php">source</a> is
1418 available on GitHub.
1422 <a href="http://vti.github.com/underscore-perl/">Underscore-perl</a>,
1423 a Perl port of many of the Underscore.js functions,
1424 aimed at on Perl hashes and arrays, also
1425 <a href="https://github.com/vti/underscore-perl/">available on GitHub</a>.
1429 <a href="https://github.com/edtsech/underscore.string">Underscore.string</a>,
1430 an Underscore extension that adds functions for string-manipulation:
1431 <tt>trim</tt>, <tt>startsWith</tt>, <tt>contains</tt>, <tt>capitalize</tt>,
1432 <tt>reverse</tt>, <tt>sprintf</tt>, and more.
1436 Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
1440 <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
1441 JavaScript with collection functions in the manner closest to Ruby's Enumerable.
1446 <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
1447 which includes comprehensive higher-order function support as well as string lambdas.
1451 Michael Aufreiter's <a href="http://substance.io/#michael/data-js">Data.js</a>,
1452 a data manipulation + persistence library for JavaScript.
1456 Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
1459 <h2 id="changelog">Change Log</h2>
1462 <b class="header">1.2.4</b> — <small><i>Jan. 4, 2012</i></small><br />
1465 You now can (and probably should) write <tt>_.chain(list)</tt>
1466 instead of <tt>_(list).chain()</tt>.
1469 Fix for escaped characters in Underscore templates, and for supporting
1470 customizations of <tt>_.templateSettings</tt> that only define one or
1471 two of the required regexes.
1474 Fix for passing an array as the first argument to an <tt>_.wrap</tt>'d function.
1477 Improved compatibility with ClojureScript, which adds a <tt>call</tt>
1478 function to <tt>String.prototype</tt>.
1484 <b class="header">1.2.3</b> — <small><i>Dec. 7, 2011</i></small><br />
1487 Dynamic scope is now preserved for compiled <tt>_.template</tt> functions,
1488 so you can use the value of <tt>this</tt> if you like.
1491 Sparse array support of <tt>_.indexOf</tt>, <tt>_.lastIndexOf</tt>.
1494 Both <tt>_.reduce</tt> and <tt>_.reduceRight</tt> can now be passed an
1495 explicitly <tt>undefined</tt> value. (There's no reason why you'd
1502 <b class="header">1.2.2</b> — <small><i>Nov. 14, 2011</i></small><br />
1505 Continued tweaks to <tt>_.isEqual</tt> semantics. Now JS primitives are
1506 considered equivalent to their wrapped versions, and arrays are compared
1507 by their numeric properties only <small>(#351)</small>.
1510 <tt>_.escape</tt> no longer tries to be smart about not double-escaping
1511 already-escaped HTML entities. Now it just escapes regardless <small>(#350)</small>.
1514 In <tt>_.template</tt>, you may now leave semicolons out of evaluated
1515 statements if you wish: <tt><% }) %></tt> <small>(#369)</small>.
1518 <tt>_.after(callback, 0)</tt> will now trigger the callback immediately,
1519 making "after" easier to use with asynchronous APIs <small>(#366)</small>.
1525 <b class="header">1.2.1</b> — <small><i>Oct. 24, 2011</i></small><br />
1528 Several important bug fixes for <tt>_.isEqual</tt>, which should now
1529 do better on mutated Arrays, and on non-Array objects with
1530 <tt>length</tt> properties. <small>(#329)</small>
1533 <b>jrburke</b> contributed Underscore exporting for AMD module loaders,
1534 and <b>tonylukasavage</b> for Appcelerator Titanium.
1535 <small>(#335, #338)</small>
1538 You can now <tt>_.groupBy(list, 'property')</tt> as a shortcut for
1539 grouping values by a particular common property.
1542 <tt>_.throttle</tt>'d functions now fire immediately upon invocation,
1543 and are rate-limited thereafter <small>(#170, #266)</small>.
1546 Most of the <tt>_.is[Type]</tt> checks no longer ducktype.
1549 The <tt>_.bind</tt> function now also works on constructors, a-la
1550 ES5 ... but you would never want to use <tt>_.bind</tt> on a
1551 constructor function.
1554 <tt>_.clone</tt> no longer wraps non-object types in Objects.
1557 <tt>_.find</tt> and <tt>_.filter</tt> are now the preferred names for
1558 <tt>_.detect</tt> and <tt>_.select</tt>.
1564 <b class="header">1.2.0</b> — <small><i>Oct. 5, 2011</i></small><br />
1567 The <tt>_.isEqual</tt> function now
1568 supports true deep equality comparisons, with checks for cyclic structures,
1569 thanks to Kit Cambridge.
1572 Underscore templates now support HTML escaping interpolations, using
1573 <tt><%- ... %></tt> syntax.
1576 Ryan Tenney contributed <tt>_.shuffle</tt>, which uses a modified
1577 Fisher-Yates to give you a shuffled copy of an array.
1580 <tt>_.uniq</tt> can now be passed an optional iterator, to determine by
1581 what criteria an object should be considered unique.
1584 <tt>_.last</tt> now takes an optional argument which will return the last
1585 N elements of the list.
1588 A new <tt>_.initial</tt> function was added, as a mirror of <tt>_.rest</tt>,
1589 which returns all the initial values of a list (except the last N).
1595 <b class="header">1.1.7</b> — <small><i>July 13, 2011</i></small><br />
1596 Added <tt>_.groupBy</tt>, which aggregates a collection into groups of like items.
1597 Added <tt>_.union</tt> and <tt>_.difference</tt>, to complement the
1598 (re-named) <tt>_.intersection</tt>.
1599 Various improvements for support of sparse arrays.
1600 <tt>_.toArray</tt> now returns a clone, if directly passed an array.
1601 <tt>_.functions</tt> now also returns the names of functions that are present
1602 in the prototype chain.
1606 <b class="header">1.1.6</b> — <small><i>April 18, 2011</i></small><br />
1607 Added <tt>_.after</tt>, which will return a function that only runs after
1608 first being called a specified number of times.
1609 <tt>_.invoke</tt> can now take a direct function reference.
1610 <tt>_.every</tt> now requires an iterator function to be passed, which
1611 mirrors the ECMA5 API.
1612 <tt>_.extend</tt> no longer copies keys when the value is undefined.
1613 <tt>_.bind</tt> now errors when trying to bind an undefined value.
1617 <b class="header">1.1.5</b> — <small><i>Mar 20, 2011</i></small><br />
1618 Added an <tt>_.defaults</tt> function, for use merging together JS objects
1619 representing default options.
1620 Added an <tt>_.once</tt> function, for manufacturing functions that should
1621 only ever execute a single time.
1622 <tt>_.bind</tt> now delegates to the native ECMAScript 5 version,
1624 <tt>_.keys</tt> now throws an error when used on non-Object values, as in
1626 Fixed a bug with <tt>_.keys</tt> when used over sparse arrays.
1630 <b class="header">1.1.4</b> — <small><i>Jan 9, 2011</i></small><br />
1631 Improved compliance with ES5's Array methods when passing <tt>null</tt>
1632 as a value. <tt>_.wrap</tt> now correctly sets <tt>this</tt> for the
1633 wrapped function. <tt>_.indexOf</tt> now takes an optional flag for
1634 finding the insertion index in an array that is guaranteed to already
1635 be sorted. Avoiding the use of <tt>.callee</tt>, to allow <tt>_.isArray</tt>
1636 to work properly in ES5's strict mode.
1640 <b class="header">1.1.3</b> — <small><i>Dec 1, 2010</i></small><br />
1641 In CommonJS, Underscore may now be required with just: <br />
1642 <tt>var _ = require("underscore")</tt>.
1643 Added <tt>_.throttle</tt> and <tt>_.debounce</tt> functions.
1644 Removed <tt>_.breakLoop</tt>, in favor of an ECMA5-style un-<i>break</i>-able
1645 each implementation — this removes the try/catch, and you'll now have
1646 better stack traces for exceptions that are thrown within an Underscore iterator.
1647 Improved the <b>isType</b> family of functions for better interoperability
1648 with Internet Explorer host objects.
1649 <tt>_.template</tt> now correctly escapes backslashes in templates.
1650 Improved <tt>_.reduce</tt> compatibility with the ECMA5 version:
1651 if you don't pass an initial value, the first item in the collection is used.
1652 <tt>_.each</tt> no longer returns the iterated collection, for improved
1653 consistency with ES5's <tt>forEach</tt>.
1657 <b class="header">1.1.2</b><br />
1658 Fixed <tt>_.contains</tt>, which was mistakenly pointing at
1659 <tt>_.intersect</tt> instead of <tt>_.include</tt>, like it should
1660 have been. Added <tt>_.unique</tt> as an alias for <tt>_.uniq</tt>.
1664 <b class="header">1.1.1</b><br />
1665 Improved the speed of <tt>_.template</tt>, and its handling of multiline
1666 interpolations. Ryan Tenney contributed optimizations to many Underscore
1667 functions. An annotated version of the source code is now available.
1671 <b class="header">1.1.0</b><br />
1672 The method signature of <tt>_.reduce</tt> has been changed to match
1673 the ECMAScript 5 signature, instead of the Ruby/Prototype.js version.
1674 This is a backwards-incompatible change. <tt>_.template</tt> may now be
1675 called with no arguments, and preserves whitespace. <tt>_.contains</tt>
1676 is a new alias for <tt>_.include</tt>.
1680 <b class="header">1.0.4</b><br />
1681 <a href="http://themoell.com/">Andri Möll</a> contributed the <tt>_.memoize</tt>
1682 function, which can be used to speed up expensive repeated computations
1683 by caching the results.
1687 <b class="header">1.0.3</b><br />
1688 Patch that makes <tt>_.isEqual</tt> return <tt>false</tt> if any property
1689 of the compared object has a <tt>NaN</tt> value. Technically the correct
1690 thing to do, but of questionable semantics. Watch out for NaN comparisons.
1694 <b class="header">1.0.2</b><br />
1695 Fixes <tt>_.isArguments</tt> in recent versions of Opera, which have
1696 arguments objects as real Arrays.
1700 <b class="header">1.0.1</b><br />
1701 Bugfix for <tt>_.isEqual</tt>, when comparing two objects with the same
1702 number of undefined keys, but with different names.
1706 <b class="header">1.0.0</b><br />
1707 Things have been stable for many months now, so Underscore is now
1708 considered to be out of beta, at <b>1.0</b>. Improvements since <b>0.6</b>
1709 include <tt>_.isBoolean</tt>, and the ability to have <tt>_.extend</tt>
1710 take multiple source objects.
1714 <b class="header">0.6.0</b><br />
1715 Major release. Incorporates a number of
1716 <a href="http://github.com/ratbeard">Mile Frawley's</a> refactors for
1717 safer duck-typing on collection functions, and cleaner internals. A new
1718 <tt>_.mixin</tt> method that allows you to extend Underscore with utility
1719 functions of your own. Added <tt>_.times</tt>, which works the same as in
1720 Ruby or Prototype.js. Native support for ECMAScript 5's <tt>Array.isArray</tt>,
1721 and <tt>Object.keys</tt>.
1725 <b class="header">0.5.8</b><br />
1726 Fixed Underscore's collection functions to work on
1727 <a href="https://developer.mozilla.org/En/DOM/NodeList">NodeLists</a> and
1728 <a href="https://developer.mozilla.org/En/DOM/HTMLCollection">HTMLCollections</a>
1729 once more, thanks to
1730 <a href="http://github.com/jmtulloss">Justin Tulloss</a>.
1734 <b class="header">0.5.7</b><br />
1735 A safer implementation of <tt>_.isArguments</tt>, and a
1736 faster <tt>_.isNumber</tt>,<br />thanks to
1737 <a href="http://jedschmidt.com/">Jed Schmidt</a>.
1741 <b class="header">0.5.6</b><br />
1742 Customizable delimiters for <tt>_.template</tt>, contributed by
1743 <a href="http://github.com/iamnoah">Noah Sloan</a>.
1747 <b class="header">0.5.5</b><br />
1748 Fix for a bug in MobileSafari's OOP-wrapper, with the arguments object.
1752 <b class="header">0.5.4</b><br />
1753 Fix for multiple single quotes within a template string for
1754 <tt>_.template</tt>. See:
1755 <a href="http://www.west-wind.com/Weblog/posts/509108.aspx">Rick Strahl's blog post</a>.
1759 <b class="header">0.5.2</b><br />
1760 New implementations of <tt>isArray</tt>, <tt>isDate</tt>, <tt>isFunction</tt>,
1761 <tt>isNumber</tt>, <tt>isRegExp</tt>, and <tt>isString</tt>, thanks to
1763 <a href="http://www.broofa.com/">Robert Kieffer</a>.
1764 Instead of doing <tt>Object#toString</tt>
1765 comparisons, they now check for expected properties, which is less safe,
1766 but more than an order of magnitude faster. Most other Underscore
1767 functions saw minor speed improvements as a result.
1768 <a href="http://dolzhenko.org/">Evgeniy Dolzhenko</a>
1769 contributed <tt>_.tap</tt>,
1770 <a href="http://ruby-doc.org/core-1.9/classes/Object.html#M000191">similar to Ruby 1.9's</a>,
1771 which is handy for injecting side effects (like logging) into chained calls.
1775 <b class="header">0.5.1</b><br />
1776 Added an <tt>_.isArguments</tt> function. Lots of little safety checks
1777 and optimizations contributed by
1778 <a href="http://github.com/iamnoah/">Noah Sloan</a> and
1779 <a href="http://themoell.com/">Andri Möll</a>.
1783 <b class="header">0.5.0</b><br />
1784 <b>[API Changes]</b> <tt>_.bindAll</tt> now takes the context object as
1785 its first parameter. If no method names are passed, all of the context
1786 object's methods are bound to it, enabling chaining and easier binding.
1787 <tt>_.functions</tt> now takes a single argument and returns the names
1788 of its Function properties. Calling <tt>_.functions(_)</tt> will get you
1789 the previous behavior.
1790 Added <tt>_.isRegExp</tt> so that <tt>isEqual</tt> can now test for RegExp equality.
1791 All of the "is" functions have been shrunk down into a single definition.
1792 <a href="http://github.com/grayrest/">Karl Guertin</a> contributed patches.
1796 <b class="header">0.4.7</b><br />
1797 Added <tt>isDate</tt>, <tt>isNaN</tt>, and <tt>isNull</tt>, for completeness.
1798 Optimizations for <tt>isEqual</tt> when checking equality between Arrays
1799 or Dates. <tt>_.keys</tt> is now <small><i><b>25%–2X</b></i></small> faster (depending on your
1800 browser) which speeds up the functions that rely on it, such as <tt>_.each</tt>.
1804 <b class="header">0.4.6</b><br />
1805 Added the <tt>range</tt> function, a port of the
1806 <a href="http://docs.python.org/library/functions.html#range">Python
1807 function of the same name</a>, for generating flexibly-numbered lists
1808 of integers. Original patch contributed by
1809 <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
1813 <b class="header">0.4.5</b><br />
1814 Added <tt>rest</tt> for Arrays and arguments objects, and aliased
1815 <tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
1816 thanks to <a href="http://github.com/lukesutton/">Luke Sutton</a>'s patches.
1817 Added tests ensuring that all Underscore Array functions also work on
1818 <i>arguments</i> objects.
1822 <b class="header">0.4.4</b><br />
1823 Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
1824 <tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
1828 <b class="header">0.4.3</b><br />
1829 Started using the native <tt>StopIteration</tt> object in browsers that support it.
1830 Fixed Underscore setup for CommonJS environments.
1834 <b class="header">0.4.2</b><br />
1835 Renamed the unwrapping function to <tt>value</tt>, for clarity.
1839 <b class="header">0.4.1</b><br />
1840 Chained Underscore objects now support the Array prototype methods, so
1841 that you can perform the full range of operations on a wrapped array
1842 without having to break your chain. Added a <tt>breakLoop</tt> method
1843 to <b>break</b> in the middle of any Underscore iteration. Added an
1844 <tt>isEmpty</tt> function that works on arrays and objects.
1848 <b class="header">0.4.0</b><br />
1849 All Underscore functions can now be called in an object-oriented style,
1850 like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
1851 <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
1852 Wrapped objects can be chained through multiple
1853 method invocations. A <a href="#functions"><tt>functions</tt></a> method
1854 was added, providing a sorted list of all the functions in Underscore.
1858 <b class="header">0.3.3</b><br />
1859 Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
1860 as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
1864 <b class="header">0.3.2</b><br />
1865 Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
1866 interpreters with: <tt>load("underscore.js")</tt>.
1867 Added <a href="#identity"><tt>identity</tt></a> as a utility function.
1871 <b class="header">0.3.1</b><br />
1872 All iterators are now passed in the original collection as their third
1873 argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
1874 objects is now called with <tt>(value, key, collection)</tt>, for details
1875 see <a href="#each"><tt>_.each</tt></a>.
1879 <b class="header">0.3.0</b><br />
1880 Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
1881 comprehensive optimizations, merged in
1882 <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
1883 <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
1884 <a href="http://narwhaljs.org/">Narwhal</a> compliant.
1888 <b class="header">0.2.0</b><br />
1889 Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
1890 <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
1891 <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
1895 <b class="header">0.1.1</b><br />
1896 Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
1901 <b class="header">0.1.0</b><br />
1902 Initial release of Underscore.js.
1906 <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
1907 <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
1915 <!-- Include Underscore, so you can play with it in the console. -->
1916 <script type="text/javascript" src="underscore.js"></script>