web-apps/vendor/underscore/docs/underscore.html
Maxim Kadushkin 741b10515d webapps added
2016-03-10 21:48:53 -03:00

3062 lines
126 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<title>underscore.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>underscore.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">&#182;</a>
</div>
<pre><code>Underscore.js 1.5.2
http://underscorejs.org
(c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters &amp; Editors
Underscore may be freely distributed under the MIT license.</code></pre>
</div>
<div class="content"><div class='highlight'><pre>(<span class="keyword">function</span>() {</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-2">&#182;</a>
</div>
<h2>Baseline setup</h2>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">&#182;</a>
</div>
<p>Establish the root object, <code>window</code> in the browser, or <code>exports</code> on the server.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> root = <span class="keyword">this</span>;</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">&#182;</a>
</div>
<p>Save the previous value of the <code>_</code> variable.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> previousUnderscore = root._;</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">&#182;</a>
</div>
<p>Establish the object that gets returned to break out of a loop iteration.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> breaker = {};</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">&#182;</a>
</div>
<p>Save bytes in the minified (but not gzipped) version:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">&#182;</a>
</div>
<p>Create quick reference variables for speed access to core prototypes.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span>
push = ArrayProto.push,
slice = ArrayProto.slice,
concat = ArrayProto.concat,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">&#182;</a>
</div>
<p>All <strong>ECMAScript 5</strong> native function implementations that we hope to use
are declared here.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span>
nativeForEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeReduce = ArrayProto.reduce,
nativeReduceRight = ArrayProto.reduceRight,
nativeFilter = ArrayProto.filter,
nativeEvery = ArrayProto.every,
nativeSome = ArrayProto.some,
nativeIndexOf = ArrayProto.indexOf,
nativeLastIndexOf = ArrayProto.lastIndexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind;</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">&#182;</a>
</div>
<p>Create a safe reference to the Underscore object for use below.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> _ = <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (obj <span class="keyword">instanceof</span> _) <span class="keyword">return</span> obj;
<span class="keyword">if</span> (!(<span class="keyword">this</span> <span class="keyword">instanceof</span> _)) <span class="keyword">return</span> <span class="keyword">new</span> _(obj);
<span class="keyword">this</span>._wrapped = obj;
};</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">&#182;</a>
</div>
<p>Export the Underscore object for <strong>Node.js</strong>, with
backwards-compatibility for the old <code>require()</code> API. If we&#39;re in
the browser, add <code>_</code> as a global object via a string identifier,
for Closure Compiler &quot;advanced&quot; mode.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (<span class="keyword">typeof</span> exports !== <span class="string">'undefined'</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> module !== <span class="string">'undefined'</span> &amp;&amp; module.exports) {
exports = module.exports = _;
}
exports._ = _;
} <span class="keyword">else</span> {
root._ = _;
}</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">&#182;</a>
</div>
<p>Current version.</p>
</div>
<div class="content"><div class='highlight'><pre> _.VERSION = <span class="string">'1.5.2'</span>;</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-12">&#182;</a>
</div>
<h2>Collection Functions</h2>
</div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">&#182;</a>
</div>
<p>The cornerstone, an <code>each</code> implementation, aka <code>forEach</code>.
Handles objects with the built-in <code>forEach</code>, arrays, and raw objects.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>forEach</code> if available.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> each = _.each = _.forEach = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span>;
<span class="keyword">if</span> (nativeForEach &amp;&amp; obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} <span class="keyword">else</span> <span class="keyword">if</span> (obj.length === +obj.length) {
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, length = obj.length; i &lt; length; i++) {
<span class="keyword">if</span> (iterator.call(context, obj[i], i, obj) === breaker) <span class="keyword">return</span>;
}
} <span class="keyword">else</span> {
<span class="keyword">var</span> keys = _.keys(obj);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, length = keys.length; i &lt; length; i++) {
<span class="keyword">if</span> (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) <span class="keyword">return</span>;
}
}
};</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">&#182;</a>
</div>
<p>Return the results of applying the iterator to each element.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>map</code> if available.</p>
</div>
<div class="content"><div class='highlight'><pre> _.map = _.collect = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">var</span> results = [];
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> results;
<span class="keyword">if</span> (nativeMap &amp;&amp; obj.map === nativeMap) <span class="keyword">return</span> obj.map(iterator, context);
each(obj, <span class="keyword">function</span>(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
<span class="keyword">return</span> results;
};
<span class="keyword">var</span> reduceError = <span class="string">'Reduce of empty array with no initial value'</span>;</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">&#182;</a>
</div>
<p><strong>Reduce</strong> builds up a single result from a list of values, aka <code>inject</code>,
or <code>foldl</code>. Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>reduce</code> if available.</p>
</div>
<div class="content"><div class='highlight'><pre> _.reduce = _.foldl = _.inject = <span class="keyword">function</span>(obj, iterator, memo, context) {
<span class="keyword">var</span> initial = arguments.length &gt; <span class="number">2</span>;
<span class="keyword">if</span> (obj == <span class="literal">null</span>) obj = [];
<span class="keyword">if</span> (nativeReduce &amp;&amp; obj.reduce === nativeReduce) {
<span class="keyword">if</span> (context) iterator = _.bind(iterator, context);
<span class="keyword">return</span> initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
}
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">if</span> (!initial) {
memo = value;
initial = <span class="literal">true</span>;
} <span class="keyword">else</span> {
memo = iterator.call(context, memo, value, index, list);
}
});
<span class="keyword">if</span> (!initial) <span class="keyword">throw</span> <span class="keyword">new</span> TypeError(reduceError);
<span class="keyword">return</span> memo;
};</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">&#182;</a>
</div>
<p>The right-associative version of reduce, also known as <code>foldr</code>.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>reduceRight</code> if available.</p>
</div>
<div class="content"><div class='highlight'><pre> _.reduceRight = _.foldr = <span class="keyword">function</span>(obj, iterator, memo, context) {
<span class="keyword">var</span> initial = arguments.length &gt; <span class="number">2</span>;
<span class="keyword">if</span> (obj == <span class="literal">null</span>) obj = [];
<span class="keyword">if</span> (nativeReduceRight &amp;&amp; obj.reduceRight === nativeReduceRight) {
<span class="keyword">if</span> (context) iterator = _.bind(iterator, context);
<span class="keyword">return</span> initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
<span class="keyword">var</span> length = obj.length;
<span class="keyword">if</span> (length !== +length) {
<span class="keyword">var</span> keys = _.keys(obj);
length = keys.length;
}
each(obj, <span class="keyword">function</span>(value, index, list) {
index = keys ? keys[--length] : --length;
<span class="keyword">if</span> (!initial) {
memo = obj[index];
initial = <span class="literal">true</span>;
} <span class="keyword">else</span> {
memo = iterator.call(context, memo, obj[index], index, list);
}
});
<span class="keyword">if</span> (!initial) <span class="keyword">throw</span> <span class="keyword">new</span> TypeError(reduceError);
<span class="keyword">return</span> memo;
};</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">&#182;</a>
</div>
<p>Return the first value which passes a truth test. Aliased as <code>detect</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.find = _.detect = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">var</span> result;
any(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">if</span> (iterator.call(context, value, index, list)) {
result = value;
<span class="keyword">return</span> <span class="literal">true</span>;
}
});
<span class="keyword">return</span> result;
};</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">&#182;</a>
</div>
<p>Return all the elements that pass a truth test.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>filter</code> if available.
Aliased as <code>select</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.filter = _.select = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">var</span> results = [];
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> results;
<span class="keyword">if</span> (nativeFilter &amp;&amp; obj.filter === nativeFilter) <span class="keyword">return</span> obj.filter(iterator, context);
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">if</span> (iterator.call(context, value, index, list)) results.push(value);
});
<span class="keyword">return</span> results;
};</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">&#182;</a>
</div>
<p>Return all the elements for which a truth test fails.</p>
</div>
<div class="content"><div class='highlight'><pre> _.reject = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">return</span> _.filter(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">return</span> !iterator.call(context, value, index, list);
}, context);
};</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">&#182;</a>
</div>
<p>Determine whether all of the elements match a truth test.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>every</code> if available.
Aliased as <code>all</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.every = _.all = <span class="keyword">function</span>(obj, iterator, context) {
iterator || (iterator = _.identity);
<span class="keyword">var</span> result = <span class="literal">true</span>;
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> result;
<span class="keyword">if</span> (nativeEvery &amp;&amp; obj.every === nativeEvery) <span class="keyword">return</span> obj.every(iterator, context);
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">if</span> (!(result = result &amp;&amp; iterator.call(context, value, index, list))) <span class="keyword">return</span> breaker;
});
<span class="keyword">return</span> !!result;
};</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">&#182;</a>
</div>
<p>Determine if at least one element in the object matches a truth test.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>some</code> if available.
Aliased as <code>any</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> any = _.some = _.any = <span class="keyword">function</span>(obj, iterator, context) {
iterator || (iterator = _.identity);
<span class="keyword">var</span> result = <span class="literal">false</span>;
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> result;
<span class="keyword">if</span> (nativeSome &amp;&amp; obj.some === nativeSome) <span class="keyword">return</span> obj.some(iterator, context);
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">if</span> (result || (result = iterator.call(context, value, index, list))) <span class="keyword">return</span> breaker;
});
<span class="keyword">return</span> !!result;
};</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">&#182;</a>
</div>
<p>Determine if the array or object contains a given value (using <code>===</code>).
Aliased as <code>include</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.contains = _.include = <span class="keyword">function</span>(obj, target) {
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> <span class="literal">false</span>;
<span class="keyword">if</span> (nativeIndexOf &amp;&amp; obj.indexOf === nativeIndexOf) <span class="keyword">return</span> obj.indexOf(target) != -<span class="number">1</span>;
<span class="keyword">return</span> any(obj, <span class="keyword">function</span>(value) {
<span class="keyword">return</span> value === target;
});
};</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">&#182;</a>
</div>
<p>Invoke a method (with arguments) on every item in a collection.</p>
</div>
<div class="content"><div class='highlight'><pre> _.invoke = <span class="keyword">function</span>(obj, method) {
<span class="keyword">var</span> args = slice.call(arguments, <span class="number">2</span>);
<span class="keyword">var</span> isFunc = _.isFunction(method);
<span class="keyword">return</span> _.map(obj, <span class="keyword">function</span>(value) {
<span class="keyword">return</span> (isFunc ? method : value[method]).apply(value, args);
});
};</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">&#182;</a>
</div>
<p>Convenience version of a common use case of <code>map</code>: fetching a property.</p>
</div>
<div class="content"><div class='highlight'><pre> _.pluck = <span class="keyword">function</span>(obj, key) {
<span class="keyword">return</span> _.map(obj, <span class="keyword">function</span>(value){ <span class="keyword">return</span> value[key]; });
};</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">&#182;</a>
</div>
<p>Convenience version of a common use case of <code>filter</code>: selecting only objects
containing specific <code>key:value</code> pairs.</p>
</div>
<div class="content"><div class='highlight'><pre> _.where = <span class="keyword">function</span>(obj, attrs, first) {
<span class="keyword">if</span> (_.isEmpty(attrs)) <span class="keyword">return</span> first ? <span class="keyword">void</span> <span class="number">0</span> : [];
<span class="keyword">return</span> _[first ? <span class="string">'find'</span> : <span class="string">'filter'</span>](obj, <span class="keyword">function</span>(value) {
<span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> attrs) {
<span class="keyword">if</span> (attrs[key] !== value[key]) <span class="keyword">return</span> <span class="literal">false</span>;
}
<span class="keyword">return</span> <span class="literal">true</span>;
});
};</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">&#182;</a>
</div>
<p>Convenience version of a common use case of <code>find</code>: getting the first object
containing specific <code>key:value</code> pairs.</p>
</div>
<div class="content"><div class='highlight'><pre> _.findWhere = <span class="keyword">function</span>(obj, attrs) {
<span class="keyword">return</span> _.where(obj, attrs, <span class="literal">true</span>);
};</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">&#182;</a>
</div>
<p>Return the maximum element or (element-based computation).
Can&#39;t optimize arrays of integers longer than 65,535 elements.
See <a href="https://bugs.webkit.org/show_bug.cgi?id=80797">WebKit Bug 80797</a></p>
</div>
<div class="content"><div class='highlight'><pre> _.max = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">if</span> (!iterator &amp;&amp; _.isArray(obj) &amp;&amp; obj[<span class="number">0</span>] === +obj[<span class="number">0</span>] &amp;&amp; obj.length &lt; <span class="number">65535</span>) {
<span class="keyword">return</span> Math.max.apply(Math, obj);
}
<span class="keyword">if</span> (!iterator &amp;&amp; _.isEmpty(obj)) <span class="keyword">return</span> -<span class="literal">Infinity</span>;
<span class="keyword">var</span> result = {computed : -<span class="literal">Infinity</span>, value: -<span class="literal">Infinity</span>};
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">var</span> computed = iterator ? iterator.call(context, value, index, list) : value;
computed &gt; result.computed &amp;&amp; (result = {value : value, computed : computed});
});
<span class="keyword">return</span> result.value;
};</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">&#182;</a>
</div>
<p>Return the minimum element (or element-based computation).</p>
</div>
<div class="content"><div class='highlight'><pre> _.min = <span class="keyword">function</span>(obj, iterator, context) {
<span class="keyword">if</span> (!iterator &amp;&amp; _.isArray(obj) &amp;&amp; obj[<span class="number">0</span>] === +obj[<span class="number">0</span>] &amp;&amp; obj.length &lt; <span class="number">65535</span>) {
<span class="keyword">return</span> Math.min.apply(Math, obj);
}
<span class="keyword">if</span> (!iterator &amp;&amp; _.isEmpty(obj)) <span class="keyword">return</span> <span class="literal">Infinity</span>;
<span class="keyword">var</span> result = {computed : <span class="literal">Infinity</span>, value: <span class="literal">Infinity</span>};
each(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">var</span> computed = iterator ? iterator.call(context, value, index, list) : value;
computed &lt; result.computed &amp;&amp; (result = {value : value, computed : computed});
});
<span class="keyword">return</span> result.value;
};</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">&#182;</a>
</div>
<p>Shuffle an array, using the modern version of the
<a href="http://en.wikipedia.org/wiki/FisherYates_shuffle">Fisher-Yates shuffle</a>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.shuffle = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> rand;
<span class="keyword">var</span> index = <span class="number">0</span>;
<span class="keyword">var</span> shuffled = [];
each(obj, <span class="keyword">function</span>(value) {
rand = _.random(index++);
shuffled[index - <span class="number">1</span>] = shuffled[rand];
shuffled[rand] = value;
});
<span class="keyword">return</span> shuffled;
};</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">&#182;</a>
</div>
<p>Sample <strong>n</strong> random values from an array.
If <strong>n</strong> is not specified, returns a single random element from the array.
The internal <code>guard</code> argument allows it to work with <code>map</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.sample = <span class="keyword">function</span>(obj, n, guard) {
<span class="keyword">if</span> (arguments.length &lt; <span class="number">2</span> || guard) {
<span class="keyword">return</span> obj[_.random(obj.length - <span class="number">1</span>)];
}
<span class="keyword">return</span> _.shuffle(obj).slice(<span class="number">0</span>, Math.max(<span class="number">0</span>, n));
};</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">&#182;</a>
</div>
<p>An internal function to generate lookup iterators.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> lookupIterator = <span class="keyword">function</span>(value) {
<span class="keyword">return</span> _.isFunction(value) ? value : <span class="keyword">function</span>(obj){ <span class="keyword">return</span> obj[value]; };
};</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">&#182;</a>
</div>
<p>Sort the object&#39;s values by a criterion produced by an iterator.</p>
</div>
<div class="content"><div class='highlight'><pre> _.sortBy = <span class="keyword">function</span>(obj, value, context) {
<span class="keyword">var</span> iterator = lookupIterator(value);
<span class="keyword">return</span> _.pluck(_.map(obj, <span class="keyword">function</span>(value, index, list) {
<span class="keyword">return</span> {
value: value,
index: index,
criteria: iterator.call(context, value, index, list)
};
}).sort(<span class="keyword">function</span>(left, right) {
<span class="keyword">var</span> a = left.criteria;
<span class="keyword">var</span> b = right.criteria;
<span class="keyword">if</span> (a !== b) {
<span class="keyword">if</span> (a &gt; b || a === <span class="keyword">void</span> <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;
<span class="keyword">if</span> (a &lt; b || b === <span class="keyword">void</span> <span class="number">0</span>) <span class="keyword">return</span> -<span class="number">1</span>;
}
<span class="keyword">return</span> left.index - right.index;
}), <span class="string">'value'</span>);
};</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">&#182;</a>
</div>
<p>An internal function used for aggregate &quot;group by&quot; operations.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> group = <span class="keyword">function</span>(behavior) {
<span class="keyword">return</span> <span class="keyword">function</span>(obj, value, context) {
<span class="keyword">var</span> result = {};
<span class="keyword">var</span> iterator = value == <span class="literal">null</span> ? _.identity : lookupIterator(value);
each(obj, <span class="keyword">function</span>(value, index) {
<span class="keyword">var</span> key = iterator.call(context, value, index, obj);
behavior(result, key, value);
});
<span class="keyword">return</span> result;
};
};</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">&#182;</a>
</div>
<p>Groups the object&#39;s values by a criterion. Pass either a string attribute
to group by, or a function that returns the criterion.</p>
</div>
<div class="content"><div class='highlight'><pre> _.groupBy = group(<span class="keyword">function</span>(result, key, value) {
(_.has(result, key) ? result[key] : (result[key] = [])).push(value);
});</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">&#182;</a>
</div>
<p>Indexes the object&#39;s values by a criterion, similar to <code>groupBy</code>, but for
when you know that your index values will be unique.</p>
</div>
<div class="content"><div class='highlight'><pre> _.indexBy = group(<span class="keyword">function</span>(result, key, value) {
result[key] = value;
});</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">&#182;</a>
</div>
<p>Counts instances of an object that group by a certain criterion. Pass
either a string attribute to count by, or a function that returns the
criterion.</p>
</div>
<div class="content"><div class='highlight'><pre> _.countBy = group(<span class="keyword">function</span>(result, key) {
_.has(result, key) ? result[key]++ : result[key] = <span class="number">1</span>;
});</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">&#182;</a>
</div>
<p>Use a comparator function to figure out the smallest index at which
an object should be inserted so as to maintain order. Uses binary search.</p>
</div>
<div class="content"><div class='highlight'><pre> _.sortedIndex = <span class="keyword">function</span>(array, obj, iterator, context) {
iterator = iterator == <span class="literal">null</span> ? _.identity : lookupIterator(iterator);
<span class="keyword">var</span> value = iterator.call(context, obj);
<span class="keyword">var</span> low = <span class="number">0</span>, high = array.length;
<span class="keyword">while</span> (low &lt; high) {
<span class="keyword">var</span> mid = (low + high) &gt;&gt;&gt; <span class="number">1</span>;
iterator.call(context, array[mid]) &lt; value ? low = mid + <span class="number">1</span> : high = mid;
}
<span class="keyword">return</span> low;
};</pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">&#182;</a>
</div>
<p>Safely create a real, live array from anything iterable.</p>
</div>
<div class="content"><div class='highlight'><pre> _.toArray = <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (!obj) <span class="keyword">return</span> [];
<span class="keyword">if</span> (_.isArray(obj)) <span class="keyword">return</span> slice.call(obj);
<span class="keyword">if</span> (obj.length === +obj.length) <span class="keyword">return</span> _.map(obj, _.identity);
<span class="keyword">return</span> _.values(obj);
};</pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">&#182;</a>
</div>
<p>Return the number of elements in an object.</p>
</div>
<div class="content"><div class='highlight'><pre> _.size = <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> <span class="number">0</span>;
<span class="keyword">return</span> (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
};</pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-40">&#182;</a>
</div>
<h2>Array Functions</h2>
</div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">&#182;</a>
</div>
<p>Get the first element of an array. Passing <strong>n</strong> will return the first N
values in the array. Aliased as <code>head</code> and <code>take</code>. The <strong>guard</strong> check
allows it to work with <code>_.map</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.first = _.head = _.take = <span class="keyword">function</span>(array, n, guard) {
<span class="keyword">if</span> (array == <span class="literal">null</span>) <span class="keyword">return</span> <span class="keyword">void</span> <span class="number">0</span>;
<span class="keyword">return</span> (n == <span class="literal">null</span>) || guard ? array[<span class="number">0</span>] : slice.call(array, <span class="number">0</span>, n);
};</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">&#182;</a>
</div>
<p>Returns everything but the last entry of the array. Especially useful on
the arguments object. Passing <strong>n</strong> will return all the values in
the array, excluding the last N. The <strong>guard</strong> check allows it to work with
<code>_.map</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.initial = <span class="keyword">function</span>(array, n, guard) {
<span class="keyword">return</span> slice.call(array, <span class="number">0</span>, array.length - ((n == <span class="literal">null</span>) || guard ? <span class="number">1</span> : n));
};</pre></div></div>
</li>
<li id="section-43">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-43">&#182;</a>
</div>
<p>Get the last element of an array. Passing <strong>n</strong> will return the last N
values in the array. The <strong>guard</strong> check allows it to work with <code>_.map</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.last = <span class="keyword">function</span>(array, n, guard) {
<span class="keyword">if</span> (array == <span class="literal">null</span>) <span class="keyword">return</span> <span class="keyword">void</span> <span class="number">0</span>;
<span class="keyword">if</span> ((n == <span class="literal">null</span>) || guard) {
<span class="keyword">return</span> array[array.length - <span class="number">1</span>];
} <span class="keyword">else</span> {
<span class="keyword">return</span> slice.call(array, Math.max(array.length - n, <span class="number">0</span>));
}
};</pre></div></div>
</li>
<li id="section-44">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-44">&#182;</a>
</div>
<p>Returns everything but the first entry of the array. Aliased as <code>tail</code> and <code>drop</code>.
Especially useful on the arguments object. Passing an <strong>n</strong> will return
the rest N values in the array. The <strong>guard</strong>
check allows it to work with <code>_.map</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.rest = _.tail = _.drop = <span class="keyword">function</span>(array, n, guard) {
<span class="keyword">return</span> slice.call(array, (n == <span class="literal">null</span>) || guard ? <span class="number">1</span> : n);
};</pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-45">&#182;</a>
</div>
<p>Trim out all falsy values from an array.</p>
</div>
<div class="content"><div class='highlight'><pre> _.compact = <span class="keyword">function</span>(array) {
<span class="keyword">return</span> _.filter(array, _.identity);
};</pre></div></div>
</li>
<li id="section-46">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-46">&#182;</a>
</div>
<p>Internal implementation of a recursive <code>flatten</code> function.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> flatten = <span class="keyword">function</span>(input, shallow, output) {
<span class="keyword">if</span> (shallow &amp;&amp; _.every(input, _.isArray)) {
<span class="keyword">return</span> concat.apply(output, input);
}
each(input, <span class="keyword">function</span>(value) {
<span class="keyword">if</span> (_.isArray(value) || _.isArguments(value)) {
shallow ? push.apply(output, value) : flatten(value, shallow, output);
} <span class="keyword">else</span> {
output.push(value);
}
});
<span class="keyword">return</span> output;
};</pre></div></div>
</li>
<li id="section-47">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-47">&#182;</a>
</div>
<p>Flatten out an array, either recursively (by default), or just one level.</p>
</div>
<div class="content"><div class='highlight'><pre> _.flatten = <span class="keyword">function</span>(array, shallow) {
<span class="keyword">return</span> flatten(array, shallow, []);
};</pre></div></div>
</li>
<li id="section-48">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-48">&#182;</a>
</div>
<p>Return a version of the array that does not contain the specified value(s).</p>
</div>
<div class="content"><div class='highlight'><pre> _.without = <span class="keyword">function</span>(array) {
<span class="keyword">return</span> _.difference(array, slice.call(arguments, <span class="number">1</span>));
};</pre></div></div>
</li>
<li id="section-49">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-49">&#182;</a>
</div>
<p>Produce a duplicate-free version of the array. If the array has already
been sorted, you have the option of using a faster algorithm.
Aliased as <code>unique</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.uniq = _.unique = <span class="keyword">function</span>(array, isSorted, iterator, context) {
<span class="keyword">if</span> (_.isFunction(isSorted)) {
context = iterator;
iterator = isSorted;
isSorted = <span class="literal">false</span>;
}
<span class="keyword">var</span> initial = iterator ? _.map(array, iterator, context) : array;
<span class="keyword">var</span> results = [];
<span class="keyword">var</span> seen = [];
each(initial, <span class="keyword">function</span>(value, index) {
<span class="keyword">if</span> (isSorted ? (!index || seen[seen.length - <span class="number">1</span>] !== value) : !_.contains(seen, value)) {
seen.push(value);
results.push(array[index]);
}
});
<span class="keyword">return</span> results;
};</pre></div></div>
</li>
<li id="section-50">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-50">&#182;</a>
</div>
<p>Produce an array that contains the union: each distinct element from all of
the passed-in arrays.</p>
</div>
<div class="content"><div class='highlight'><pre> _.union = <span class="keyword">function</span>() {
<span class="keyword">return</span> _.uniq(_.flatten(arguments, <span class="literal">true</span>));
};</pre></div></div>
</li>
<li id="section-51">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-51">&#182;</a>
</div>
<p>Produce an array that contains every item shared between all the
passed-in arrays.</p>
</div>
<div class="content"><div class='highlight'><pre> _.intersection = <span class="keyword">function</span>(array) {
<span class="keyword">var</span> rest = slice.call(arguments, <span class="number">1</span>);
<span class="keyword">return</span> _.filter(_.uniq(array), <span class="keyword">function</span>(item) {
<span class="keyword">return</span> _.every(rest, <span class="keyword">function</span>(other) {
<span class="keyword">return</span> _.indexOf(other, item) &gt;= <span class="number">0</span>;
});
});
};</pre></div></div>
</li>
<li id="section-52">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-52">&#182;</a>
</div>
<p>Take the difference between one array and a number of other arrays.
Only the elements present in just the first array will remain.</p>
</div>
<div class="content"><div class='highlight'><pre> _.difference = <span class="keyword">function</span>(array) {
<span class="keyword">var</span> rest = concat.apply(ArrayProto, slice.call(arguments, <span class="number">1</span>));
<span class="keyword">return</span> _.filter(array, <span class="keyword">function</span>(value){ <span class="keyword">return</span> !_.contains(rest, value); });
};</pre></div></div>
</li>
<li id="section-53">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-53">&#182;</a>
</div>
<p>Zip together multiple lists into a single array -- elements that share
an index go together.</p>
</div>
<div class="content"><div class='highlight'><pre> _.zip = <span class="keyword">function</span>() {
<span class="keyword">var</span> length = _.max(_.pluck(arguments, <span class="string">"length"</span>).concat(<span class="number">0</span>));
<span class="keyword">var</span> results = <span class="keyword">new</span> Array(length);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; length; i++) {
results[i] = _.pluck(arguments, <span class="string">''</span> + i);
}
<span class="keyword">return</span> results;
};</pre></div></div>
</li>
<li id="section-54">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-54">&#182;</a>
</div>
<p>Converts lists into objects. Pass either a single array of <code>[key, value]</code>
pairs, or two parallel arrays of the same length -- one of keys, and one of
the corresponding values.</p>
</div>
<div class="content"><div class='highlight'><pre> _.object = <span class="keyword">function</span>(list, values) {
<span class="keyword">if</span> (list == <span class="literal">null</span>) <span class="keyword">return</span> {};
<span class="keyword">var</span> result = {};
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, length = list.length; i &lt; length; i++) {
<span class="keyword">if</span> (values) {
result[list[i]] = values[i];
} <span class="keyword">else</span> {
result[list[i][<span class="number">0</span>]] = list[i][<span class="number">1</span>];
}
}
<span class="keyword">return</span> result;
};</pre></div></div>
</li>
<li id="section-55">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-55">&#182;</a>
</div>
<p>If the browser doesn&#39;t supply us with indexOf (I&#39;m looking at you, <strong>MSIE</strong>),
we need this function. Return the position of the first occurrence of an
item in an array, or -1 if the item is not included in the array.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>indexOf</code> if available.
If the array is large and already in sort order, pass <code>true</code>
for <strong>isSorted</strong> to use binary search.</p>
</div>
<div class="content"><div class='highlight'><pre> _.indexOf = <span class="keyword">function</span>(array, item, isSorted) {
<span class="keyword">if</span> (array == <span class="literal">null</span>) <span class="keyword">return</span> -<span class="number">1</span>;
<span class="keyword">var</span> i = <span class="number">0</span>, length = array.length;
<span class="keyword">if</span> (isSorted) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> isSorted == <span class="string">'number'</span>) {
i = (isSorted &lt; <span class="number">0</span> ? Math.max(<span class="number">0</span>, length + isSorted) : isSorted);
} <span class="keyword">else</span> {
i = _.sortedIndex(array, item);
<span class="keyword">return</span> array[i] === item ? i : -<span class="number">1</span>;
}
}
<span class="keyword">if</span> (nativeIndexOf &amp;&amp; array.indexOf === nativeIndexOf) <span class="keyword">return</span> array.indexOf(item, isSorted);
<span class="keyword">for</span> (; i &lt; length; i++) <span class="keyword">if</span> (array[i] === item) <span class="keyword">return</span> i;
<span class="keyword">return</span> -<span class="number">1</span>;
};</pre></div></div>
</li>
<li id="section-56">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-56">&#182;</a>
</div>
<p>Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>lastIndexOf</code> if available.</p>
</div>
<div class="content"><div class='highlight'><pre> _.lastIndexOf = <span class="keyword">function</span>(array, item, from) {
<span class="keyword">if</span> (array == <span class="literal">null</span>) <span class="keyword">return</span> -<span class="number">1</span>;
<span class="keyword">var</span> hasIndex = from != <span class="literal">null</span>;
<span class="keyword">if</span> (nativeLastIndexOf &amp;&amp; array.lastIndexOf === nativeLastIndexOf) {
<span class="keyword">return</span> hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
}
<span class="keyword">var</span> i = (hasIndex ? from : array.length);
<span class="keyword">while</span> (i--) <span class="keyword">if</span> (array[i] === item) <span class="keyword">return</span> i;
<span class="keyword">return</span> -<span class="number">1</span>;
};</pre></div></div>
</li>
<li id="section-57">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-57">&#182;</a>
</div>
<p>Generate an integer Array containing an arithmetic progression. A port of
the native Python <code>range()</code> function. See
<a href="http://docs.python.org/library/functions.html#range">the Python documentation</a>.</p>
</div>
<div class="content"><div class='highlight'><pre> _.range = <span class="keyword">function</span>(start, stop, step) {
<span class="keyword">if</span> (arguments.length &lt;= <span class="number">1</span>) {
stop = start || <span class="number">0</span>;
start = <span class="number">0</span>;
}
step = arguments[<span class="number">2</span>] || <span class="number">1</span>;
<span class="keyword">var</span> length = Math.max(Math.ceil((stop - start) / step), <span class="number">0</span>);
<span class="keyword">var</span> idx = <span class="number">0</span>;
<span class="keyword">var</span> range = <span class="keyword">new</span> Array(length);
<span class="keyword">while</span>(idx &lt; length) {
range[idx++] = start;
start += step;
}
<span class="keyword">return</span> range;
};</pre></div></div>
</li>
<li id="section-58">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-58">&#182;</a>
</div>
<h2>Function (ahem) Functions</h2>
</div>
</li>
<li id="section-59">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-59">&#182;</a>
</div>
<p>Reusable constructor function for prototype setting.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> ctor = <span class="keyword">function</span>(){};</pre></div></div>
</li>
<li id="section-60">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-60">&#182;</a>
</div>
<p>Create a function bound to a given object (assigning <code>this</code>, and arguments,
optionally). Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>Function.bind</code> if
available.</p>
</div>
<div class="content"><div class='highlight'><pre> _.bind = <span class="keyword">function</span>(func, context) {
<span class="keyword">var</span> args, bound;
<span class="keyword">if</span> (nativeBind &amp;&amp; func.bind === nativeBind) <span class="keyword">return</span> nativeBind.apply(func, slice.call(arguments, <span class="number">1</span>));
<span class="keyword">if</span> (!_.isFunction(func)) <span class="keyword">throw</span> <span class="keyword">new</span> TypeError;
args = slice.call(arguments, <span class="number">2</span>);
<span class="keyword">return</span> bound = <span class="keyword">function</span>() {
<span class="keyword">if</span> (!(<span class="keyword">this</span> <span class="keyword">instanceof</span> bound)) <span class="keyword">return</span> func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
<span class="keyword">var</span> self = <span class="keyword">new</span> ctor;
ctor.prototype = <span class="literal">null</span>;
<span class="keyword">var</span> result = func.apply(self, args.concat(slice.call(arguments)));
<span class="keyword">if</span> (Object(result) === result) <span class="keyword">return</span> result;
<span class="keyword">return</span> self;
};
};</pre></div></div>
</li>
<li id="section-61">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-61">&#182;</a>
</div>
<p>Partially apply a function by creating a version that has had some of its
arguments pre-filled, without changing its dynamic <code>this</code> context.</p>
</div>
<div class="content"><div class='highlight'><pre> _.partial = <span class="keyword">function</span>(func) {
<span class="keyword">var</span> args = slice.call(arguments, <span class="number">1</span>);
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">return</span> func.apply(<span class="keyword">this</span>, args.concat(slice.call(arguments)));
};
};</pre></div></div>
</li>
<li id="section-62">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-62">&#182;</a>
</div>
<p>Bind all of an object&#39;s methods to that object. Useful for ensuring that
all callbacks defined on an object belong to it.</p>
</div>
<div class="content"><div class='highlight'><pre> _.bindAll = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> funcs = slice.call(arguments, <span class="number">1</span>);
<span class="keyword">if</span> (funcs.length === <span class="number">0</span>) <span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">"bindAll must be passed function names"</span>);
each(funcs, <span class="keyword">function</span>(f) { obj[f] = _.bind(obj[f], obj); });
<span class="keyword">return</span> obj;
};</pre></div></div>
</li>
<li id="section-63">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-63">&#182;</a>
</div>
<p>Memoize an expensive function by storing its results.</p>
</div>
<div class="content"><div class='highlight'><pre> _.memoize = <span class="keyword">function</span>(func, hasher) {
<span class="keyword">var</span> memo = {};
hasher || (hasher = _.identity);
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">var</span> key = hasher.apply(<span class="keyword">this</span>, arguments);
<span class="keyword">return</span> _.has(memo, key) ? memo[key] : (memo[key] = func.apply(<span class="keyword">this</span>, arguments));
};
};</pre></div></div>
</li>
<li id="section-64">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-64">&#182;</a>
</div>
<p>Delays a function for the given number of milliseconds, and then calls
it with the arguments supplied.</p>
</div>
<div class="content"><div class='highlight'><pre> _.delay = <span class="keyword">function</span>(func, wait) {
<span class="keyword">var</span> args = slice.call(arguments, <span class="number">2</span>);
<span class="keyword">return</span> setTimeout(<span class="keyword">function</span>(){ <span class="keyword">return</span> func.apply(<span class="literal">null</span>, args); }, wait);
};</pre></div></div>
</li>
<li id="section-65">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-65">&#182;</a>
</div>
<p>Defers a function, scheduling it to run after the current call stack has
cleared.</p>
</div>
<div class="content"><div class='highlight'><pre> _.defer = <span class="keyword">function</span>(func) {
<span class="keyword">return</span> _.delay.apply(_, [func, <span class="number">1</span>].concat(slice.call(arguments, <span class="number">1</span>)));
};</pre></div></div>
</li>
<li id="section-66">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-66">&#182;</a>
</div>
<p>Returns a function, that, when invoked, will only be triggered at most once
during a given window of time. Normally, the throttled function will run
as much as it can, without ever going more than once per <code>wait</code> duration;
but if you&#39;d like to disable the execution on the leading edge, pass
<code>{leading: false}</code>. To disable execution on the trailing edge, ditto.</p>
</div>
<div class="content"><div class='highlight'><pre> _.throttle = <span class="keyword">function</span>(func, wait, options) {
<span class="keyword">var</span> context, args, result;
<span class="keyword">var</span> timeout = <span class="literal">null</span>;
<span class="keyword">var</span> previous = <span class="number">0</span>;
options || (options = {});
<span class="keyword">var</span> later = <span class="keyword">function</span>() {
previous = options.leading === <span class="literal">false</span> ? <span class="number">0</span> : <span class="keyword">new</span> Date;
timeout = <span class="literal">null</span>;
result = func.apply(context, args);
};
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">var</span> now = <span class="keyword">new</span> Date;
<span class="keyword">if</span> (!previous &amp;&amp; options.leading === <span class="literal">false</span>) previous = now;
<span class="keyword">var</span> remaining = wait - (now - previous);
context = <span class="keyword">this</span>;
args = arguments;
<span class="keyword">if</span> (remaining &lt;= <span class="number">0</span>) {
clearTimeout(timeout);
timeout = <span class="literal">null</span>;
previous = now;
result = func.apply(context, args);
} <span class="keyword">else</span> <span class="keyword">if</span> (!timeout &amp;&amp; options.trailing !== <span class="literal">false</span>) {
timeout = setTimeout(later, remaining);
}
<span class="keyword">return</span> result;
};
};</pre></div></div>
</li>
<li id="section-67">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-67">&#182;</a>
</div>
<p>Returns a function, that, as long as it continues to be invoked, will not
be triggered. The function will be called after it stops being called for
N milliseconds. If <code>immediate</code> is passed, trigger the function on the
leading edge, instead of the trailing.</p>
</div>
<div class="content"><div class='highlight'><pre> _.debounce = <span class="keyword">function</span>(func, wait, immediate) {
<span class="keyword">var</span> timeout, args, context, timestamp, result;
<span class="keyword">return</span> <span class="keyword">function</span>() {
context = <span class="keyword">this</span>;
args = arguments;
timestamp = <span class="keyword">new</span> Date();
<span class="keyword">var</span> later = <span class="keyword">function</span>() {
<span class="keyword">var</span> last = (<span class="keyword">new</span> Date()) - timestamp;
<span class="keyword">if</span> (last &lt; wait) {
timeout = setTimeout(later, wait - last);
} <span class="keyword">else</span> {
timeout = <span class="literal">null</span>;
<span class="keyword">if</span> (!immediate) result = func.apply(context, args);
}
};
<span class="keyword">var</span> callNow = immediate &amp;&amp; !timeout;
<span class="keyword">if</span> (!timeout) {
timeout = setTimeout(later, wait);
}
<span class="keyword">if</span> (callNow) result = func.apply(context, args);
<span class="keyword">return</span> result;
};
};</pre></div></div>
</li>
<li id="section-68">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-68">&#182;</a>
</div>
<p>Returns a function that will be executed at most one time, no matter how
often you call it. Useful for lazy initialization.</p>
</div>
<div class="content"><div class='highlight'><pre> _.once = <span class="keyword">function</span>(func) {
<span class="keyword">var</span> ran = <span class="literal">false</span>, memo;
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">if</span> (ran) <span class="keyword">return</span> memo;
ran = <span class="literal">true</span>;
memo = func.apply(<span class="keyword">this</span>, arguments);
func = <span class="literal">null</span>;
<span class="keyword">return</span> memo;
};
};</pre></div></div>
</li>
<li id="section-69">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-69">&#182;</a>
</div>
<p>Returns the first function passed as an argument to the second,
allowing you to adjust arguments, run code before and after, and
conditionally execute the original function.</p>
</div>
<div class="content"><div class='highlight'><pre> _.wrap = <span class="keyword">function</span>(func, wrapper) {
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">var</span> args = [func];
push.apply(args, arguments);
<span class="keyword">return</span> wrapper.apply(<span class="keyword">this</span>, args);
};
};</pre></div></div>
</li>
<li id="section-70">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-70">&#182;</a>
</div>
<p>Returns a function that is the composition of a list of functions, each
consuming the return value of the function that follows.</p>
</div>
<div class="content"><div class='highlight'><pre> _.compose = <span class="keyword">function</span>() {
<span class="keyword">var</span> funcs = arguments;
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">var</span> args = arguments;
<span class="keyword">for</span> (<span class="keyword">var</span> i = funcs.length - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) {
args = [funcs[i].apply(<span class="keyword">this</span>, args)];
}
<span class="keyword">return</span> args[<span class="number">0</span>];
};
};</pre></div></div>
</li>
<li id="section-71">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-71">&#182;</a>
</div>
<p>Returns a function that will only be executed after being called N times.</p>
</div>
<div class="content"><div class='highlight'><pre> _.after = <span class="keyword">function</span>(times, func) {
<span class="keyword">return</span> <span class="keyword">function</span>() {
<span class="keyword">if</span> (--times &lt; <span class="number">1</span>) {
<span class="keyword">return</span> func.apply(<span class="keyword">this</span>, arguments);
}
};
};</pre></div></div>
</li>
<li id="section-72">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-72">&#182;</a>
</div>
<h2>Object Functions</h2>
</div>
</li>
<li id="section-73">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-73">&#182;</a>
</div>
<p>Retrieve the names of an object&#39;s properties.
Delegates to <strong>ECMAScript 5</strong>&#39;s native <code>Object.keys</code></p>
</div>
<div class="content"><div class='highlight'><pre> _.keys = nativeKeys || <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (obj !== Object(obj)) <span class="keyword">throw</span> <span class="keyword">new</span> TypeError(<span class="string">'Invalid object'</span>);
<span class="keyword">var</span> keys = [];
<span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> obj) <span class="keyword">if</span> (_.has(obj, key)) keys.push(key);
<span class="keyword">return</span> keys;
};</pre></div></div>
</li>
<li id="section-74">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-74">&#182;</a>
</div>
<p>Retrieve the values of an object&#39;s properties.</p>
</div>
<div class="content"><div class='highlight'><pre> _.values = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> keys = _.keys(obj);
<span class="keyword">var</span> length = keys.length;
<span class="keyword">var</span> values = <span class="keyword">new</span> Array(length);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; length; i++) {
values[i] = obj[keys[i]];
}
<span class="keyword">return</span> values;
};</pre></div></div>
</li>
<li id="section-75">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-75">&#182;</a>
</div>
<p>Convert an object into a list of <code>[key, value]</code> pairs.</p>
</div>
<div class="content"><div class='highlight'><pre> _.pairs = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> keys = _.keys(obj);
<span class="keyword">var</span> length = keys.length;
<span class="keyword">var</span> pairs = <span class="keyword">new</span> Array(length);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; length; i++) {
pairs[i] = [keys[i], obj[keys[i]]];
}
<span class="keyword">return</span> pairs;
};</pre></div></div>
</li>
<li id="section-76">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-76">&#182;</a>
</div>
<p>Invert the keys and values of an object. The values must be serializable.</p>
</div>
<div class="content"><div class='highlight'><pre> _.invert = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> result = {};
<span class="keyword">var</span> keys = _.keys(obj);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, length = keys.length; i &lt; length; i++) {
result[obj[keys[i]]] = keys[i];
}
<span class="keyword">return</span> result;
};</pre></div></div>
</li>
<li id="section-77">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-77">&#182;</a>
</div>
<p>Return a sorted list of the function names available on the object.
Aliased as <code>methods</code></p>
</div>
<div class="content"><div class='highlight'><pre> _.functions = _.methods = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> names = [];
<span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> obj) {
<span class="keyword">if</span> (_.isFunction(obj[key])) names.push(key);
}
<span class="keyword">return</span> names.sort();
};</pre></div></div>
</li>
<li id="section-78">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-78">&#182;</a>
</div>
<p>Extend a given object with all the properties in passed-in object(s).</p>
</div>
<div class="content"><div class='highlight'><pre> _.extend = <span class="keyword">function</span>(obj) {
each(slice.call(arguments, <span class="number">1</span>), <span class="keyword">function</span>(source) {
<span class="keyword">if</span> (source) {
<span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> source) {
obj[prop] = source[prop];
}
}
});
<span class="keyword">return</span> obj;
};</pre></div></div>
</li>
<li id="section-79">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-79">&#182;</a>
</div>
<p>Return a copy of the object only containing the whitelisted properties.</p>
</div>
<div class="content"><div class='highlight'><pre> _.pick = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> copy = {};
<span class="keyword">var</span> keys = concat.apply(ArrayProto, slice.call(arguments, <span class="number">1</span>));
each(keys, <span class="keyword">function</span>(key) {
<span class="keyword">if</span> (key <span class="keyword">in</span> obj) copy[key] = obj[key];
});
<span class="keyword">return</span> copy;
};</pre></div></div>
</li>
<li id="section-80">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-80">&#182;</a>
</div>
<p>Return a copy of the object without the blacklisted properties.</p>
</div>
<div class="content"><div class='highlight'><pre> _.omit = <span class="keyword">function</span>(obj) {
<span class="keyword">var</span> copy = {};
<span class="keyword">var</span> keys = concat.apply(ArrayProto, slice.call(arguments, <span class="number">1</span>));
<span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> obj) {
<span class="keyword">if</span> (!_.contains(keys, key)) copy[key] = obj[key];
}
<span class="keyword">return</span> copy;
};</pre></div></div>
</li>
<li id="section-81">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-81">&#182;</a>
</div>
<p>Fill in a given object with default properties.</p>
</div>
<div class="content"><div class='highlight'><pre> _.defaults = <span class="keyword">function</span>(obj) {
each(slice.call(arguments, <span class="number">1</span>), <span class="keyword">function</span>(source) {
<span class="keyword">if</span> (source) {
<span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> source) {
<span class="keyword">if</span> (obj[prop] === <span class="keyword">void</span> <span class="number">0</span>) obj[prop] = source[prop];
}
}
});
<span class="keyword">return</span> obj;
};</pre></div></div>
</li>
<li id="section-82">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-82">&#182;</a>
</div>
<p>Create a (shallow-cloned) duplicate of an object.</p>
</div>
<div class="content"><div class='highlight'><pre> _.clone = <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (!_.isObject(obj)) <span class="keyword">return</span> obj;
<span class="keyword">return</span> _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};</pre></div></div>
</li>
<li id="section-83">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-83">&#182;</a>
</div>
<p>Invokes interceptor with the obj, and then returns obj.
The primary purpose of this method is to &quot;tap into&quot; a method chain, in
order to perform operations on intermediate results within the chain.</p>
</div>
<div class="content"><div class='highlight'><pre> _.tap = <span class="keyword">function</span>(obj, interceptor) {
interceptor(obj);
<span class="keyword">return</span> obj;
};</pre></div></div>
</li>
<li id="section-84">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-84">&#182;</a>
</div>
<p>Internal recursive comparison function for <code>isEqual</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> eq = <span class="keyword">function</span>(a, b, aStack, bStack) {</pre></div></div>
</li>
<li id="section-85">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-85">&#182;</a>
</div>
<p>Identical objects are equal. <code>0 === -0</code>, but they aren&#39;t identical.
See the <a href="http://wiki.ecmascript.org/doku.php?id=harmony:egal">Harmony <code>egal</code> proposal</a>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (a === b) <span class="keyword">return</span> a !== <span class="number">0</span> || <span class="number">1</span> / a == <span class="number">1</span> / b;</pre></div></div>
</li>
<li id="section-86">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-86">&#182;</a>
</div>
<p>A strict comparison is necessary because <code>null == undefined</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (a == <span class="literal">null</span> || b == <span class="literal">null</span>) <span class="keyword">return</span> a === b;</pre></div></div>
</li>
<li id="section-87">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-87">&#182;</a>
</div>
<p>Unwrap any wrapped objects.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (a <span class="keyword">instanceof</span> _) a = a._wrapped;
<span class="keyword">if</span> (b <span class="keyword">instanceof</span> _) b = b._wrapped;</pre></div></div>
</li>
<li id="section-88">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-88">&#182;</a>
</div>
<p>Compare <code>[[Class]]</code> names.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> className = toString.call(a);
<span class="keyword">if</span> (className != toString.call(b)) <span class="keyword">return</span> <span class="literal">false</span>;
<span class="keyword">switch</span> (className) {</pre></div></div>
</li>
<li id="section-89">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-89">&#182;</a>
</div>
<p>Strings, numbers, dates, and booleans are compared by value.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">case</span> <span class="string">'[object String]'</span>:</pre></div></div>
</li>
<li id="section-90">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-90">&#182;</a>
</div>
<p>Primitives and their corresponding object wrappers are equivalent; thus, <code>&quot;5&quot;</code> is
equivalent to <code>new String(&quot;5&quot;)</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">return</span> a == String(b);
<span class="keyword">case</span> <span class="string">'[object Number]'</span>:</pre></div></div>
</li>
<li id="section-91">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-91">&#182;</a>
</div>
<p><code>NaN</code>s are equivalent, but non-reflexive. An <code>egal</code> comparison is performed for
other numeric values.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">return</span> a != +a ? b != +b : (a == <span class="number">0</span> ? <span class="number">1</span> / a == <span class="number">1</span> / b : a == +b);
<span class="keyword">case</span> <span class="string">'[object Date]'</span>:
<span class="keyword">case</span> <span class="string">'[object Boolean]'</span>:</pre></div></div>
</li>
<li id="section-92">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-92">&#182;</a>
</div>
<p>Coerce dates and booleans to numeric primitive values. Dates are compared by their
millisecond representations. Note that invalid dates with millisecond representations
of <code>NaN</code> are not equivalent.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">return</span> +a == +b;</pre></div></div>
</li>
<li id="section-93">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-93">&#182;</a>
</div>
<p>RegExps are compared by their source patterns and flags.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">case</span> <span class="string">'[object RegExp]'</span>:
<span class="keyword">return</span> a.source == b.source &amp;&amp;
a.global == b.global &amp;&amp;
a.multiline == b.multiline &amp;&amp;
a.ignoreCase == b.ignoreCase;
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> a != <span class="string">'object'</span> || <span class="keyword">typeof</span> b != <span class="string">'object'</span>) <span class="keyword">return</span> <span class="literal">false</span>;</pre></div></div>
</li>
<li id="section-94">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-94">&#182;</a>
</div>
<p>Assume equality for cyclic structures. The algorithm for detecting cyclic
structures is adapted from ES 5.1 section 15.12.3, abstract operation <code>JO</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> length = aStack.length;
<span class="keyword">while</span> (length--) {</pre></div></div>
</li>
<li id="section-95">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-95">&#182;</a>
</div>
<p>Linear search. Performance is inversely proportional to the number of
unique nested structures.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (aStack[length] == a) <span class="keyword">return</span> bStack[length] == b;
}</pre></div></div>
</li>
<li id="section-96">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-96">&#182;</a>
</div>
<p>Objects with different constructors are not equivalent, but <code>Object</code>s
from different frames are.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> aCtor = a.constructor, bCtor = b.constructor;
<span class="keyword">if</span> (aCtor !== bCtor &amp;&amp; !(_.isFunction(aCtor) &amp;&amp; (aCtor <span class="keyword">instanceof</span> aCtor) &amp;&amp;
_.isFunction(bCtor) &amp;&amp; (bCtor <span class="keyword">instanceof</span> bCtor))) {
<span class="keyword">return</span> <span class="literal">false</span>;
}</pre></div></div>
</li>
<li id="section-97">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-97">&#182;</a>
</div>
<p>Add the first object to the stack of traversed objects.</p>
</div>
<div class="content"><div class='highlight'><pre> aStack.push(a);
bStack.push(b);
<span class="keyword">var</span> size = <span class="number">0</span>, result = <span class="literal">true</span>;</pre></div></div>
</li>
<li id="section-98">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-98">&#182;</a>
</div>
<p>Recursively compare objects and arrays.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (className == <span class="string">'[object Array]'</span>) {</pre></div></div>
</li>
<li id="section-99">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-99">&#182;</a>
</div>
<p>Compare array lengths to determine if a deep comparison is necessary.</p>
</div>
<div class="content"><div class='highlight'><pre> size = a.length;
result = size == b.length;
<span class="keyword">if</span> (result) {</pre></div></div>
</li>
<li id="section-100">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-100">&#182;</a>
</div>
<p>Deep compare the contents, ignoring non-numeric properties.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">while</span> (size--) {
<span class="keyword">if</span> (!(result = eq(a[size], b[size], aStack, bStack))) <span class="keyword">break</span>;
}
}
} <span class="keyword">else</span> {</pre></div></div>
</li>
<li id="section-101">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-101">&#182;</a>
</div>
<p>Deep compare objects.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> a) {
<span class="keyword">if</span> (_.has(a, key)) {</pre></div></div>
</li>
<li id="section-102">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-102">&#182;</a>
</div>
<p>Count the expected number of properties.</p>
</div>
<div class="content"><div class='highlight'><pre> size++;</pre></div></div>
</li>
<li id="section-103">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-103">&#182;</a>
</div>
<p>Deep compare each member.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (!(result = _.has(b, key) &amp;&amp; eq(a[key], b[key], aStack, bStack))) <span class="keyword">break</span>;
}
}</pre></div></div>
</li>
<li id="section-104">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-104">&#182;</a>
</div>
<p>Ensure that both objects contain the same number of properties.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (result) {
<span class="keyword">for</span> (key <span class="keyword">in</span> b) {
<span class="keyword">if</span> (_.has(b, key) &amp;&amp; !(size--)) <span class="keyword">break</span>;
}
result = !size;
}
}</pre></div></div>
</li>
<li id="section-105">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-105">&#182;</a>
</div>
<p>Remove the first object from the stack of traversed objects.</p>
</div>
<div class="content"><div class='highlight'><pre> aStack.pop();
bStack.pop();
<span class="keyword">return</span> result;
};</pre></div></div>
</li>
<li id="section-106">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-106">&#182;</a>
</div>
<p>Perform a deep comparison to check if two objects are equal.</p>
</div>
<div class="content"><div class='highlight'><pre> _.isEqual = <span class="keyword">function</span>(a, b) {
<span class="keyword">return</span> eq(a, b, [], []);
};</pre></div></div>
</li>
<li id="section-107">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-107">&#182;</a>
</div>
<p>Is a given array, string, or object empty?
An &quot;empty&quot; object has no enumerable own-properties.</p>
</div>
<div class="content"><div class='highlight'><pre> _.isEmpty = <span class="keyword">function</span>(obj) {
<span class="keyword">if</span> (obj == <span class="literal">null</span>) <span class="keyword">return</span> <span class="literal">true</span>;
<span class="keyword">if</span> (_.isArray(obj) || _.isString(obj)) <span class="keyword">return</span> obj.length === <span class="number">0</span>;
<span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> obj) <span class="keyword">if</span> (_.has(obj, key)) <span class="keyword">return</span> <span class="literal">false</span>;
<span class="keyword">return</span> <span class="literal">true</span>;
};</pre></div></div>
</li>
<li id="section-108">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-108">&#182;</a>
</div>
<p>Is a given value a DOM element?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isElement = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> !!(obj &amp;&amp; obj.nodeType === <span class="number">1</span>);
};</pre></div></div>
</li>
<li id="section-109">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-109">&#182;</a>
</div>
<p>Is a given value an array?
Delegates to ECMA5&#39;s native Array.isArray</p>
</div>
<div class="content"><div class='highlight'><pre> _.isArray = nativeIsArray || <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> toString.call(obj) == <span class="string">'[object Array]'</span>;
};</pre></div></div>
</li>
<li id="section-110">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-110">&#182;</a>
</div>
<p>Is a given variable an object?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isObject = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> obj === Object(obj);
};</pre></div></div>
</li>
<li id="section-111">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-111">&#182;</a>
</div>
<p>Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.</p>
</div>
<div class="content"><div class='highlight'><pre> each([<span class="string">'Arguments'</span>, <span class="string">'Function'</span>, <span class="string">'String'</span>, <span class="string">'Number'</span>, <span class="string">'Date'</span>, <span class="string">'RegExp'</span>], <span class="keyword">function</span>(name) {
_[<span class="string">'is'</span> + name] = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> toString.call(obj) == <span class="string">'[object '</span> + name + <span class="string">']'</span>;
};
});</pre></div></div>
</li>
<li id="section-112">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-112">&#182;</a>
</div>
<p>Define a fallback version of the method in browsers (ahem, IE), where
there isn&#39;t any inspectable &quot;Arguments&quot; type.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (!_.isArguments(arguments)) {
_.isArguments = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> !!(obj &amp;&amp; _.has(obj, <span class="string">'callee'</span>));
};
}</pre></div></div>
</li>
<li id="section-113">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-113">&#182;</a>
</div>
<p>Optimize <code>isFunction</code> if appropriate.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (<span class="keyword">typeof</span> (<span class="regexp">/./</span>) !== <span class="string">'function'</span>) {
_.isFunction = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> <span class="keyword">typeof</span> obj === <span class="string">'function'</span>;
};
}</pre></div></div>
</li>
<li id="section-114">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-114">&#182;</a>
</div>
<p>Is a given object a finite number?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isFinite = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> isFinite(obj) &amp;&amp; !isNaN(parseFloat(obj));
};</pre></div></div>
</li>
<li id="section-115">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-115">&#182;</a>
</div>
<p>Is the given value <code>NaN</code>? (NaN is the only number which does not equal itself).</p>
</div>
<div class="content"><div class='highlight'><pre> _.isNaN = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> _.isNumber(obj) &amp;&amp; obj != +obj;
};</pre></div></div>
</li>
<li id="section-116">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-116">&#182;</a>
</div>
<p>Is a given value a boolean?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isBoolean = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> obj === <span class="literal">true</span> || obj === <span class="literal">false</span> || toString.call(obj) == <span class="string">'[object Boolean]'</span>;
};</pre></div></div>
</li>
<li id="section-117">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-117">&#182;</a>
</div>
<p>Is a given value equal to null?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isNull = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> obj === <span class="literal">null</span>;
};</pre></div></div>
</li>
<li id="section-118">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-118">&#182;</a>
</div>
<p>Is a given variable undefined?</p>
</div>
<div class="content"><div class='highlight'><pre> _.isUndefined = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> obj === <span class="keyword">void</span> <span class="number">0</span>;
};</pre></div></div>
</li>
<li id="section-119">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-119">&#182;</a>
</div>
<p>Shortcut function for checking if an object has a given property directly
on itself (in other words, not on a prototype).</p>
</div>
<div class="content"><div class='highlight'><pre> _.has = <span class="keyword">function</span>(obj, key) {
<span class="keyword">return</span> hasOwnProperty.call(obj, key);
};</pre></div></div>
</li>
<li id="section-120">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-120">&#182;</a>
</div>
<h2>Utility Functions</h2>
</div>
</li>
<li id="section-121">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-121">&#182;</a>
</div>
<p>Run Underscore.js in <em>noConflict</em> mode, returning the <code>_</code> variable to its
previous owner. Returns a reference to the Underscore object.</p>
</div>
<div class="content"><div class='highlight'><pre> _.noConflict = <span class="keyword">function</span>() {
root._ = previousUnderscore;
<span class="keyword">return</span> <span class="keyword">this</span>;
};</pre></div></div>
</li>
<li id="section-122">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-122">&#182;</a>
</div>
<p>Keep the identity function around for default iterators.</p>
</div>
<div class="content"><div class='highlight'><pre> _.identity = <span class="keyword">function</span>(value) {
<span class="keyword">return</span> value;
};</pre></div></div>
</li>
<li id="section-123">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-123">&#182;</a>
</div>
<p>Run a function <strong>n</strong> times.</p>
</div>
<div class="content"><div class='highlight'><pre> _.times = <span class="keyword">function</span>(n, iterator, context) {
<span class="keyword">var</span> accum = Array(Math.max(<span class="number">0</span>, n));
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; n; i++) accum[i] = iterator.call(context, i);
<span class="keyword">return</span> accum;
};</pre></div></div>
</li>
<li id="section-124">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-124">&#182;</a>
</div>
<p>Return a random integer between min and max (inclusive).</p>
</div>
<div class="content"><div class='highlight'><pre> _.random = <span class="keyword">function</span>(min, max) {
<span class="keyword">if</span> (max == <span class="literal">null</span>) {
max = min;
min = <span class="number">0</span>;
}
<span class="keyword">return</span> min + Math.floor(Math.random() * (max - min + <span class="number">1</span>));
};</pre></div></div>
</li>
<li id="section-125">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-125">&#182;</a>
</div>
<p>List of HTML entities for escaping.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> entityMap = {
escape: {
<span class="string">'&amp;'</span>: <span class="string">'&amp;amp;'</span>,
<span class="string">'&lt;'</span>: <span class="string">'&amp;lt;'</span>,
<span class="string">'&gt;'</span>: <span class="string">'&amp;gt;'</span>,
<span class="string">'"'</span>: <span class="string">'&amp;quot;'</span>,
<span class="string">"'"</span>: <span class="string">'&amp;#x27;'</span>
}
};
entityMap.unescape = _.invert(entityMap.escape);</pre></div></div>
</li>
<li id="section-126">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-126">&#182;</a>
</div>
<p>Regexes containing the keys and values listed immediately above.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> entityRegexes = {
escape: <span class="keyword">new</span> RegExp(<span class="string">'['</span> + _.keys(entityMap.escape).join(<span class="string">''</span>) + <span class="string">']'</span>, <span class="string">'g'</span>),
unescape: <span class="keyword">new</span> RegExp(<span class="string">'('</span> + _.keys(entityMap.unescape).join(<span class="string">'|'</span>) + <span class="string">')'</span>, <span class="string">'g'</span>)
};</pre></div></div>
</li>
<li id="section-127">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-127">&#182;</a>
</div>
<p>Functions for escaping and unescaping strings to/from HTML interpolation.</p>
</div>
<div class="content"><div class='highlight'><pre> _.each([<span class="string">'escape'</span>, <span class="string">'unescape'</span>], <span class="keyword">function</span>(method) {
_[method] = <span class="keyword">function</span>(string) {
<span class="keyword">if</span> (string == <span class="literal">null</span>) <span class="keyword">return</span> <span class="string">''</span>;
<span class="keyword">return</span> (<span class="string">''</span> + string).replace(entityRegexes[method], <span class="keyword">function</span>(match) {
<span class="keyword">return</span> entityMap[method][match];
});
};
});</pre></div></div>
</li>
<li id="section-128">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-128">&#182;</a>
</div>
<p>If the value of the named <code>property</code> is a function then invoke it with the
<code>object</code> as context; otherwise, return it.</p>
</div>
<div class="content"><div class='highlight'><pre> _.result = <span class="keyword">function</span>(object, property) {
<span class="keyword">if</span> (object == <span class="literal">null</span>) <span class="keyword">return</span> <span class="keyword">void</span> <span class="number">0</span>;
<span class="keyword">var</span> value = object[property];
<span class="keyword">return</span> _.isFunction(value) ? value.call(object) : value;
};</pre></div></div>
</li>
<li id="section-129">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-129">&#182;</a>
</div>
<p>Add your own custom functions to the Underscore object.</p>
</div>
<div class="content"><div class='highlight'><pre> _.mixin = <span class="keyword">function</span>(obj) {
each(_.functions(obj), <span class="keyword">function</span>(name) {
<span class="keyword">var</span> func = _[name] = obj[name];
_.prototype[name] = <span class="keyword">function</span>() {
<span class="keyword">var</span> args = [<span class="keyword">this</span>._wrapped];
push.apply(args, arguments);
<span class="keyword">return</span> result.call(<span class="keyword">this</span>, func.apply(_, args));
};
});
};</pre></div></div>
</li>
<li id="section-130">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-130">&#182;</a>
</div>
<p>Generate a unique integer id (unique within the entire client session).
Useful for temporary DOM ids.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> idCounter = <span class="number">0</span>;
_.uniqueId = <span class="keyword">function</span>(prefix) {
<span class="keyword">var</span> id = ++idCounter + <span class="string">''</span>;
<span class="keyword">return</span> prefix ? prefix + id : id;
};</pre></div></div>
</li>
<li id="section-131">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-131">&#182;</a>
</div>
<p>By default, Underscore uses ERB-style template delimiters, change the
following template settings to use alternative delimiters.</p>
</div>
<div class="content"><div class='highlight'><pre> _.templateSettings = {
evaluate : <span class="regexp">/&lt;%([\s\S]+?)%&gt;/g</span>,
interpolate : <span class="regexp">/&lt;%=([\s\S]+?)%&gt;/g</span>,
escape : <span class="regexp">/&lt;%-([\s\S]+?)%&gt;/g</span>
};</pre></div></div>
</li>
<li id="section-132">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-132">&#182;</a>
</div>
<p>When customizing <code>templateSettings</code>, if you don&#39;t want to define an
interpolation, evaluation or escaping regex, we need one that is
guaranteed not to match.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> noMatch = <span class="regexp">/(.)^/</span>;</pre></div></div>
</li>
<li id="section-133">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-133">&#182;</a>
</div>
<p>Certain characters need to be escaped so that they can be put into a
string literal.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> escapes = {
<span class="string">"'"</span>: <span class="string">"'"</span>,
<span class="string">'\\'</span>: <span class="string">'\\'</span>,
<span class="string">'\r'</span>: <span class="string">'r'</span>,
<span class="string">'\n'</span>: <span class="string">'n'</span>,
<span class="string">'\t'</span>: <span class="string">'t'</span>,
<span class="string">'\u2028'</span>: <span class="string">'u2028'</span>,
<span class="string">'\u2029'</span>: <span class="string">'u2029'</span>
};
<span class="keyword">var</span> escaper = <span class="regexp">/\\|'|\r|\n|\t|\u2028|\u2029/g</span>;</pre></div></div>
</li>
<li id="section-134">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-134">&#182;</a>
</div>
<p>JavaScript micro-templating, similar to John Resig&#39;s implementation.
Underscore templating handles arbitrary delimiters, preserves whitespace,
and correctly escapes quotes within interpolated code.</p>
</div>
<div class="content"><div class='highlight'><pre> _.template = <span class="keyword">function</span>(text, data, settings) {
<span class="keyword">var</span> render;
settings = _.defaults({}, settings, _.templateSettings);</pre></div></div>
</li>
<li id="section-135">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-135">&#182;</a>
</div>
<p>Combine delimiters into one regular expression via alternation.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> matcher = <span class="keyword">new</span> RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join(<span class="string">'|'</span>) + <span class="string">'|$'</span>, <span class="string">'g'</span>);</pre></div></div>
</li>
<li id="section-136">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-136">&#182;</a>
</div>
<p>Compile the template source, escaping string literals appropriately.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> index = <span class="number">0</span>;
<span class="keyword">var</span> source = <span class="string">"__p+='"</span>;
text.replace(matcher, <span class="keyword">function</span>(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, <span class="keyword">function</span>(match) { <span class="keyword">return</span> <span class="string">'\\'</span> + escapes[match]; });
<span class="keyword">if</span> (escape) {
source += <span class="string">"'+\n((__t=("</span> + escape + <span class="string">"))==null?'':_.escape(__t))+\n'"</span>;
}
<span class="keyword">if</span> (interpolate) {
source += <span class="string">"'+\n((__t=("</span> + interpolate + <span class="string">"))==null?'':__t)+\n'"</span>;
}
<span class="keyword">if</span> (evaluate) {
source += <span class="string">"';\n"</span> + evaluate + <span class="string">"\n__p+='"</span>;
}
index = offset + match.length;
<span class="keyword">return</span> match;
});
source += <span class="string">"';\n"</span>;</pre></div></div>
</li>
<li id="section-137">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-137">&#182;</a>
</div>
<p>If a variable is not specified, place data values in local scope.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> (!settings.variable) source = <span class="string">'with(obj||{}){\n'</span> + source + <span class="string">'}\n'</span>;
source = <span class="string">"var __t,__p='',__j=Array.prototype.join,"</span> +
<span class="string">"print=function(){__p+=__j.call(arguments,'');};\n"</span> +
source + <span class="string">"return __p;\n"</span>;
<span class="keyword">try</span> {
render = <span class="keyword">new</span> Function(settings.variable || <span class="string">'obj'</span>, <span class="string">'_'</span>, source);
} <span class="keyword">catch</span> (e) {
e.source = source;
<span class="keyword">throw</span> e;
}
<span class="keyword">if</span> (data) <span class="keyword">return</span> render(data, _);
<span class="keyword">var</span> template = <span class="keyword">function</span>(data) {
<span class="keyword">return</span> render.call(<span class="keyword">this</span>, data, _);
};</pre></div></div>
</li>
<li id="section-138">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-138">&#182;</a>
</div>
<p>Provide the compiled function source as a convenience for precompilation.</p>
</div>
<div class="content"><div class='highlight'><pre> template.source = <span class="string">'function('</span> + (settings.variable || <span class="string">'obj'</span>) + <span class="string">'){\n'</span> + source + <span class="string">'}'</span>;
<span class="keyword">return</span> template;
};</pre></div></div>
</li>
<li id="section-139">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-139">&#182;</a>
</div>
<p>Add a &quot;chain&quot; function, which will delegate to the wrapper.</p>
</div>
<div class="content"><div class='highlight'><pre> _.chain = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> _(obj).chain();
};</pre></div></div>
</li>
<li id="section-140">
<div class="annotation">
<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-140">&#182;</a>
</div>
<h2>OOP</h2>
</div>
</li>
<li id="section-141">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-141">&#182;</a>
</div>
<p>If Underscore is called as a function, it returns a wrapped object that
can be used OO-style. This wrapper holds altered versions of all the
underscore functions. Wrapped objects may be chained.</p>
<p>Helper function to continue chaining intermediate results.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> result = <span class="keyword">function</span>(obj) {
<span class="keyword">return</span> <span class="keyword">this</span>._chain ? _(obj).chain() : obj;
};</pre></div></div>
</li>
<li id="section-142">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-142">&#182;</a>
</div>
<p>Add all of the Underscore functions to the wrapper object.</p>
</div>
<div class="content"><div class='highlight'><pre> _.mixin(_);</pre></div></div>
</li>
<li id="section-143">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-143">&#182;</a>
</div>
<p>Add all mutator Array functions to the wrapper.</p>
</div>
<div class="content"><div class='highlight'><pre> each([<span class="string">'pop'</span>, <span class="string">'push'</span>, <span class="string">'reverse'</span>, <span class="string">'shift'</span>, <span class="string">'sort'</span>, <span class="string">'splice'</span>, <span class="string">'unshift'</span>], <span class="keyword">function</span>(name) {
<span class="keyword">var</span> method = ArrayProto[name];
_.prototype[name] = <span class="keyword">function</span>() {
<span class="keyword">var</span> obj = <span class="keyword">this</span>._wrapped;
method.apply(obj, arguments);
<span class="keyword">if</span> ((name == <span class="string">'shift'</span> || name == <span class="string">'splice'</span>) &amp;&amp; obj.length === <span class="number">0</span>) <span class="keyword">delete</span> obj[<span class="number">0</span>];
<span class="keyword">return</span> result.call(<span class="keyword">this</span>, obj);
};
});</pre></div></div>
</li>
<li id="section-144">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-144">&#182;</a>
</div>
<p>Add all accessor Array functions to the wrapper.</p>
</div>
<div class="content"><div class='highlight'><pre> each([<span class="string">'concat'</span>, <span class="string">'join'</span>, <span class="string">'slice'</span>], <span class="keyword">function</span>(name) {
<span class="keyword">var</span> method = ArrayProto[name];
_.prototype[name] = <span class="keyword">function</span>() {
<span class="keyword">return</span> result.call(<span class="keyword">this</span>, method.apply(<span class="keyword">this</span>._wrapped, arguments));
};
});
_.extend(_.prototype, {</pre></div></div>
</li>
<li id="section-145">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-145">&#182;</a>
</div>
<p>Start chaining a wrapped Underscore object.</p>
</div>
<div class="content"><div class='highlight'><pre> chain: <span class="keyword">function</span>() {
<span class="keyword">this</span>._chain = <span class="literal">true</span>;
<span class="keyword">return</span> <span class="keyword">this</span>;
},</pre></div></div>
</li>
<li id="section-146">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-146">&#182;</a>
</div>
<p>Extracts the result from a wrapped and chained object.</p>
</div>
<div class="content"><div class='highlight'><pre> value: <span class="keyword">function</span>() {
<span class="keyword">return</span> <span class="keyword">this</span>._wrapped;
}
});
}).call(<span class="keyword">this</span>);</pre></div></div>
</li>
</ul>
</div>
</body>
</html>