adding breakLoop

This commit is contained in:
Jeremy Ashkenas
2009-11-08 12:07:10 -05:00
parent cda4612a00
commit 5eec4e5d22
3 changed files with 146 additions and 118 deletions

View File

@@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Underscore.js</title>
<title>Underscore.js</title>
<style>
body {
font-size: 16px;
@@ -65,45 +65,45 @@
</style>
</head>
<body>
<div class="container">
<h1>Underscore.js</h1>
<p>
<a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
<a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
utility-belt library for JavaScript that provides a lot of the
functional programming support that you would expect in
<a href="http://prototypejs.org/api">Prototype.js</a>
(or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
but without extending any of the built-in JavaScript objects. It's the
<a href="http://prototypejs.org/api">Prototype.js</a>
(or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
but without extending any of the built-in JavaScript objects. It's the
tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
</p>
<p>
Underscore provides 45-odd functions that support both the usual
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
as well as more specialized helpers: function binding, javascript
templating, deep equality testing, and so on. It delegates to built-in
functions, if present, so
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">JavaScript 1.6</a>
compliant browsers will use the
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
Underscore provides 45-odd functions that support both the usual
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
as well as more specialized helpers: function binding, javascript
templating, deep equality testing, and so on. It delegates to built-in
functions, if present, so
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">JavaScript 1.6</a>
compliant browsers will use the
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
<b>every</b>, <b>some</b> and <b>indexOf</b>.
</p>
<p>
Underscore includes a complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
for your perusal.
</p>
<p>
The unabridged source code is
The unabridged source code is
<a href="http://github.com/documentcloud/underscore/">available on GitHub</a>.
</p>
<h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
<p>
<table>
<tr>
@@ -116,28 +116,28 @@
</tr>
</table>
</p>
<h2 id="styles">Object-Oriented and Functional Styles</h2>
<p>
You can use Underscore in either an object-oriented or a functional style,
depending on your preference. The following two lines of code are
You can use Underscore in either an object-oriented or a functional style,
depending on your preference. The following two lines of code are
identical ways to double a list of numbers.
</p>
<pre>
_.map([1, 2, 3], function(n){ return n * 2; });
_([1, 2, 3]).map(function(n){ return n * 2; });</pre>
<p>
Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use <tt>get</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of
together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song.
</p>
<pre>
var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"},
@@ -149,73 +149,73 @@ var lyrics = [
_(lyrics).chain()
.map(function(line) { return line.words.split(' '); })
.flatten()
.reduce({}, function(counts, word) {
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}).get();
=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
<h2>Table of Contents</h2>
<p>
<b>Collections</b>
<br />
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
<a href="#detect">detect</a>, <a href="#select">select</a>,
<a href="#reject">reject</a>, <a href="#all">all</a>,
<a href="#any">any</a>, <a href="#include">include</a>,
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
<a href="#max">max</a>, <a href="#min">min</a>,
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<br />
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
<a href="#detect">detect</a>, <a href="#select">select</a>,
<a href="#reject">reject</a>, <a href="#all">all</a>,
<a href="#any">any</a>, <a href="#include">include</a>,
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
<a href="#max">max</a>, <a href="#min">min</a>,
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<a href="#toArray">toArray</a>, <a href="#size">size</a></span>
</p>
<p>
<b>Arrays</b>
<br />
<span class="methods"><a href="#first">first</a>, <a href="#last">last</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
<span class="methods"><a href="#first">first</a>, <a href="#last">last</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
<a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>,
<a href="#lastIndexOf">lastIndexOf</a></span>
</p>
<p>
<b>Functions</b>
<br />
<span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
<span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
<a href="#defer">defer</a>, <a href="#wrap">wrap</a></span>, <a href="#compose">compose</a></span>
</p>
<p>
<b>Objects</b>
<br />
<span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
<a href="#extend">extend</a>, <a href="#clone">clone</a>,
<a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
<span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
<a href="#extend">extend</a>, <a href="#clone">clone</a>,
<a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
<a href="#isArray">isArray</a>, <a href="#isFunction">isFunction</a>, <a href="#isUndefined">isUndefined</a>
</span>
</p>
<p>
<b>Utility</b>
<br />
<span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#identity">identity</a>, <a href="#uniqueId">uniqueId</a>,
<a href="#template">template</a></span>
<a href="#identity">identity</a>, <a href="#breakLoop">breakLoop</a></span>,
<a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
</p>
<p>
<b>Chaining</b>
<br />
<span class="methods"><a href="#chain">chain</a>, <a href="#get">get</a>
</p>
<div id="documentation">
<h2>Collection Functions (Arrays or Objects)</h2>
<p id="each">
<b class="header">each</b><code>_.each(list, iterator, [context])</code>
<span class="alias">Alias: <b>forEach</b></span>
@@ -224,19 +224,19 @@ _(lyrics).chain()
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
passed. Each invocation of <b>iterator</b> is called with three arguments:
<tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b>
method of its own, it will be used instead. Delegates to the native
arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b>
method of its own, it will be used instead. Delegates to the native
<b>forEach</b> function if it exists.
</p>
<pre>
_.each([1, 2, 3], function(num){ alert(num); });
=&gt; alerts each number in turn...</pre>
<p id="map">
<b class="header">map</b><code>_.map(list, iterator, [context])</code>
<br />
Produces a new array of values by mapping each value in <b>list</b>
through a transformation function (<b>iterator</b>). If the native
Produces a new array of values by mapping each value in <b>list</b>
through a transformation function (<b>iterator</b>). If the native
<b>map</b> method exists, it will be used instead.
</p>
<pre>
@@ -247,7 +247,7 @@ _.map([1, 2, 3], function(num){ return num * 3 });
<b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
<span class="alias">Aliases: <b>inject, foldl</b></span>
<br />
Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
<b>list</b> of values into a single value. <b>Memo</b> is the initial state
of the reduction, and each successive step of it should be returned by
<b>iterator</b>.
@@ -261,7 +261,7 @@ var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
<b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code>
<span class="alias">Alias: <b>foldr</b></span>
<br />
The right-associative version of <b>reduce</b>. Delegates to the
The right-associative version of <b>reduce</b>. Delegates to the
JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
is not as useful in JavaScript as it would be in a language with lazy
evaluation.
@@ -275,8 +275,8 @@ var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); });
<p id="detect">
<b class="header">detect</b><code>_.detect(list, iterator, [context])</code>
<br />
Looks through each value in the <b>list</b>, returning the first one that
passes a truth test (<b>iterator</b>). The function returns as
Looks through each value in the <b>list</b>, returning the first one that
passes a truth test (<b>iterator</b>). The function returns as
soon as it finds an acceptable element, and doesn't traverse the
entire list.
</p>
@@ -314,7 +314,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
<span class="alias">Alias: <b>every</b></span>
<br />
Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
truth test. If an <b>iterator</b> is not provided, the truthy value of
truth test. If an <b>iterator</b> is not provided, the truthy value of
the element will be used instead. Delegates to the native method <b>every</b>, if
present.
</p>
@@ -364,7 +364,7 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
<p id="pluck">
<b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
<br />
An convenient version of what is perhaps the most common use-case for
An convenient version of what is perhaps the most common use-case for
<b>map</b>: extracting a list of property values.
</p>
<pre>
@@ -443,9 +443,9 @@ _.sortedIndex([10, 20, 30, 40, 50], 35);
_.size({one : 1, two : 2, three : 3});
=&gt; 3
</pre>
<h2>Array Functions</h2>
<p id="first">
<b class="header">first</b><code>_.first(array)</code>
<br />
@@ -469,8 +469,8 @@ _.last([3, 2, 1]);
<p id="compact">
<b class="header">compact</b><code>_.compact(array)</code>
<br />
Returns a copy of the <b>array</b> with all falsy values removed.
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
Returns a copy of the <b>array</b> with all falsy values removed.
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
<i>undefined</i> and <i>NaN</i> are all falsy.
</p>
<pre>
@@ -537,7 +537,7 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
<p id="indexOf">
<b class="header">indexOf</b><code>_.indexOf(array, value)</code>
<br />
Returns the index at which <b>value</b> can be found in the <b>array</b>,
Returns the index at which <b>value</b> can be found in the <b>array</b>,
or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
<b>indexOf</b> function unless it's missing.
</p>
@@ -549,8 +549,8 @@ _.indexOf([1, 2, 3], 2);
<p id="lastIndexOf">
<b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
<br />
Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
function if possible.
</p>
<pre>
@@ -566,7 +566,7 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
Bind a <b>function</b> to a <b>context</b> object, meaning that whenever
the function is called, the value of <i>this</i> will be the <b>context</b>.
Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
also known as <b>currying</b>.
also known as <b>currying</b>.
</p>
<pre>
var func = function(greeting){ return greeting + ': ' + this.name };
@@ -578,15 +578,15 @@ func();
<p id="bindAll">
<b class="header">bindAll</b><code>_.bindAll(*methodNames, context)</code>
<br />
Binds a number of methods on the <b>context</b> object, specified by
Binds a number of methods on the <b>context</b> object, specified by
<b>methodNames</b>, to be run in the context of that object whenever they
are invoked. Very handy for binding functions that are going to be used
as event handlers, which would otherwise be invoked with a fairly useless
as event handlers, which would otherwise be invoked with a fairly useless
<i>this</i>.
</p>
<pre>
var buttonView = {
label : 'underscore',
label : 'underscore',
onClick : function(){ alert('clicked: ' + this.label); },
onHover : function(){ console.log('hovering: ' + this.label); }
};
@@ -624,8 +624,8 @@ _.defer(function(){ alert('deferred'); });
<p id="wrap">
<b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
<br />
Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
passing it as the first argument. This allows the <b>wrapper</b> to
Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
passing it as the first argument. This allows the <b>wrapper</b> to
execute code before and after the <b>function</b> runs, adjust the arguments,
and execute it conditionally.
</p>
@@ -641,7 +641,7 @@ hello();
<p id="compose">
<b class="header">compose</b><code>_.compose(*functions)</code>
<br />
Returns the composition of a list of <b>functions</b>, where each function
Returns the composition of a list of <b>functions</b>, where each function
consumes the return value of the function that follows. In math terms,
composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
<i>f(g(h()))</i>.
@@ -679,7 +679,7 @@ _.values({one : 1, two : 2, three : 3});
<p id="extend">
<b class="header">extend</b><code>_.extend(destination, source)</code>
<br />
Copy all of the properties in the <b>source</b> object over to the
Copy all of the properties in the <b>source</b> object over to the
<b>destination</b> object.
</p>
<pre>
@@ -768,7 +768,7 @@ _.isUndefined(window.missingVariable);
</pre>
<h2>Utility Functions</h2>
<p id="noConflict">
<b class="header">noConflict</b><code>_.noConflict()</code>
<br />
@@ -781,20 +781,34 @@ var underscore = _.noConflict();</pre>
<p id="identity">
<b class="header">identity</b><code>_.identity(value)</code>
<br />
Returns the same value that is used as the argument. In math:
Returns the same value that is used as the argument. In math:
<tt>f(x) = x</tt><br />
This function looks useless, but is used throughout Underscore as
This function looks useless, but is used throughout Underscore as
a default iterator.
</p>
<pre>
var moe = {name : 'moe'};
moe === _.identity(moe);
=> true</pre>
=&gt; true</pre>
<p id="breakLoop">
<b class="header">breakLoop</b><code>_.breakLoop()</code>
<br />
Breaks out of the current loop iteration. Similar to the <tt>break</tt>
keyword in regular "for" loop, but works within an iterator function.
</p>
<pre>
var result = null;
_.each([1, 2, 3], function(num) {
if ((result = num) == 2) _.breakLoop();
});
result;
=&gt; 2</pre>
<p id="uniqueId">
<b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
<br />
Generate a globally-unique id for client-side models or DOM elements
Generate a globally-unique id for client-side models or DOM elements
that need one. If <b>prefix</b> is passed, the id will be appended to it.
</p>
<pre>
@@ -819,7 +833,7 @@ _.functions();
Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
data sources. Template functions can both interpolate variables, using<br />
<i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary JavaScript code, with
<i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary JavaScript code, with
<i>&lt;% &hellip; %&gt;</i>. When you evaluate a template function, pass in a
<b>context</b> object that has properties corresponding to the template's free
variables. If you're writing a one-off, you can pass the <b>context</b>
@@ -834,10 +848,10 @@ compiled({name : 'moe'});
var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
_.template(list, {people : ['moe', 'curly', 'larry']});
=&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
</pre>
</pre>
<h2>Chaining</h2>
<p id="chain">
<b class="header">chain</b><code>_(obj).chain()</code>
<br />
@@ -861,30 +875,30 @@ _([1, 2, 3]).get();
</pre>
<h2>Change Log</h2>
<p>
<b class="header">0.4.0</b><br />
All Underscore functions can now be called in an object-oriented style,
All Underscore functions can now be called in an object-oriented style,
like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
<a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
<a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
Wrapped objects can be chained through multiple
method invocations. A <a href="#functions"><tt>functions</tt></a> method
method invocations. A <a href="#functions"><tt>functions</tt></a> method
was added, providing a sorted list of all the functions in Underscore.
</p>
<p>
<b class="header">0.3.3</b><br />
Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
</p>
<p>
<b class="header">0.3.2</b><br />
Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
interpreters with: <tt>load("underscore.js")</tt>.
Added <a href="#identity"><tt>identity</tt></a> as a utility function.
</p>
<p>
<b class="header">0.3.1</b><br />
All iterators are now passed in the original collection as their third
@@ -892,29 +906,29 @@ _([1, 2, 3]).get();
objects is now called with <tt>(value, key, collection)</tt>, for details
see <a href="#each"><tt>_.each</tt></a>.
</p>
<p>
<b class="header">0.3.0</b><br />
Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
comprehensive optimizations, merged in
<a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
<a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
comprehensive optimizations, merged in
<a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
<a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
<a href="http://narwhaljs.org/">Narwhal</a> compliant.
</p>
<p>
<b class="header">0.2.0</b><br />
Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
<tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
<tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
<tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
</p>
<p>
<b class="header">0.1.1</b><br />
Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
other variables.
</p>
<p>
<b class="header">0.1.0</b><br />
Initial release of Underscore.js.
@@ -925,9 +939,9 @@ _([1, 2, 3]).get();
<img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
</a>
</p>
</div>
</div>
</body>

View File

@@ -14,6 +14,15 @@ $(document).ready(function() {
var moe = {name : 'moe'};
equals(_.identity(moe), moe, 'moe is the same as his identity');
});
test('utility: breakLoop', function() {
var result = null;
_([1,2,3,4,5,6]).each(function(num) {
result = num;
if (num == 3) _.breakLoop();
});
equals(result, 3, 'broke out of a loop');
});
test("utility: uniqueId", function() {
var ids = [], i = 0;
@@ -22,7 +31,7 @@ $(document).ready(function() {
});
test("utility: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "clone", "compact", "compose",
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",

View File

@@ -90,7 +90,7 @@
_.each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) {
result = value;
throw '__break__';
_.breakLoop();
}
});
return result;
@@ -123,7 +123,7 @@
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) throw '__break__';
if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
});
return result;
};
@@ -135,7 +135,7 @@
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index, list) {
if (result = iterator.call(context, value, index, list)) throw '__break__';
if (result = iterator.call(context, value, index, list)) _.breakLoop();
});
return result;
};
@@ -146,7 +146,7 @@
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
var found = false;
_.each(obj, function(value) {
if (found = value === target) throw '__break__';
if (found = value === target) _.breakLoop();
});
return found;
};
@@ -446,6 +446,11 @@
return value;
};
// Break out of the middle of an iteration.
_.breakLoop = function() {
throw "__break__";
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var idCounter = 0;