Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: sstephenson/prototype
...
head fork: sstephenson/prototype
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 37 additions and 2 deletions.
  1. +37 −2 src/prototype/lang/enumerable.js
View
39 src/prototype/lang/enumerable.js
@@ -5,7 +5,7 @@
* objects that act as collections of values. It is a cornerstone of
* Prototype.
*
- * [[Enumerable]] is a _mixin_: a set of methods intended not for standaone
+ * [[Enumerable]] is a _mixin_: a set of methods intended not for standalone
* use, but for incorporation into other objects.
*
* Prototype mixes [[Enumerable]] into several classes. The most visible cases
@@ -30,7 +30,42 @@
*
* If there is no `context` argument, the iterator function will execute in
* the scope from which the [[Enumerable]] method itself was called.
- *
+ *
+ * ##### Flow control
+ *
+ * You might find yourself missing the `break` and `continue` keywords that
+ * are available in ordinary `for` loops. If you need to break out of an
+ * enumeration before it's done, you can throw a special object named
+ * `$break`:
+ *
+ * var myObject = {};
+ *
+ * ['foo', 'bar', 'baz', 'thud'].each( function(name, index) {
+ * if (name === 'baz') throw $break;
+ * myObject[name] = index;
+ * });
+ *
+ * myObject;
+ * // -> { foo: 0, bar: 1 }
+ *
+ * Though we're technically throwing an exception, the `each` method knows
+ * to catch a thrown `$break` object and treat it as a command to stop
+ * iterating. (_Any_ exception thrown within an iterator will stop
+ * iteration, but only `$break` will be caught and suppressed.)
+ *
+ * If you need `continue`-like behavior, you can simply return early from
+ * your iterator:
+ *
+ * var myObject = {};
+ *
+ * ['foo', 'bar', 'baz', 'thud'].each( function(name, index) {
+ * if (name === 'baz') return;
+ * myObject[name] = index;
+ * });
+ *
+ * myObject;
+ * // -> { foo: 0, bar: 1, thud: 3 }
+ *
* ##### Mixing [[Enumerable]] into your own objects
*
* So, let's say you've created your very own collection-like object (say,

No commit comments for this range

Something went wrong with that request. Please try again.