AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 579


In using this example, we implicitly used an iterator. JavaScript 1.7 allows you access to
this object to form your own iterations instead of using the standard for...in. In order to get
to the next iteration, the next() method is called. When there are no objects left, the
StopIteration exception is thrown.

function showYield()
{
var call=1;
yield call;
call++;
yield call;
call++;
yield call;
}
var myGenerator = showYield();
var myIterator = Iterator(myGenerator);
try
{
while(true)
alert(myIterator.next());
}
catch(err if err instanceof StopIteration)
{
alert("Finished");
}

While these features are still exclusive to JavaScript 1.7, they suggest big changes ahead.

NNOT EOTE JavaScript 1.8 makes some changes to these ideas with the introduction of expression closures
and generator expressions. Given this version is still in flux at this time of writing, we point
readers to the developer.mozilla.org site for the latest syntax changes.

Functions


Currently, the function serves as the main approach to encapsulating flow logic in
JavaScript for programming in the large. The general syntax of a function is:

function identifier( [ arg1 [, arg2 [, ... ] ] ] )
{
statements
}

From within a function you can return a value using the return statement.

return [expression];

If expression is omitted, the function returns undefined. A small example is shown here.

function timesTwo(x)
{
alert("x = "+x);
Free download pdf