Prototype Enumerable each() Method


Advertisements


This method is the cornerstone of Enumerable. It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing the chain-calling.

The optional context parameter is what the iterator function will be bound to. If used, the this keyword inside the iterator will point to the object given by the argument.

Syntax

Iterator.find([context]);

Return Value

  • Returns an enumerable element

Example

<html>
<head>
<title>Prototype examples</title>
<script type="text/javascript" 
   src="/javascript/prototype.js">
</script>

<script>

function showResult(){
   ['one', 'two', 'three'].each(function(s) {
     alert("First loop : " + s);
   });

   [ 'hello', 'world'].each(function(s, index) {
     alert("Second Loop : " +  index + ': ' + s);
   });
}

</script>
</head>

<body>

  <p>Click the button to see the result.</p>
  <br />
  <br />
  <input type="button" value="Result" onclick="showResult();"/>

</body>
</html>

To understand it in better way you can Try it yourself.


prototype_enumerating.htm

Advertisements