Copyright © tutorialspoint.com

Prototype Enumerable each() Method

previous next


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 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 :

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.


previous next

Copyright © tutorialspoint.com