mozilla

Compare Revisions

Array.prototype.forEach()

Change Revisions

Revision 1538:

Revision 1538 by Anonymous on

Revision 1539:

Revision 1539 by Erik1 on

Title:
Array forEach method
Array forEach method
Slug:
JavaScript/Reference/Global_Objects/Array/forEach
JavaScript/Reference/Global_Objects/Array/forEach
Tags:
javascript, ECMAScript5, "JavaScript 1.6"
javascript, ECMAScript5, "JavaScript 1.6"
Content:

Revision 1538
Revision 1539
n102      <i>TODO</i>n102      The following code creates a simple writer object and then 
 >uses the <code>writeln</code> method to write one line per elemen
 >t in the array. (This presumes the presence of a <code>print</cod
 >e> function to call!):
tt104    <pre>
105var writer = {
106    sb:       [],
107    write:    function (s) {
108        this.sb.join(s);
109    },
110    writeln:  function (s) {
111        this.write(s + "\n");
112    },
113    toString: function () {
114        return this.sb.join("");
115    }
116};
117 
118[2, 5, 9].forEach(writer.writeln, writer);
119print(writer.toString());
120 
121// Prints:
122// 2
123// 5
124// 9
125</pre>

Back to History