//*** This code is copyright 2003 by Gavin Kistner, !@phrogz.net //*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt //*** Reuse or modification is free provided you abide by the terms of that license. //*** (Including the first two lines above in your source code satisfies the conditions.) Array.prototype.applyToEach = function(f){ for (var i=0,len=this.length-1;i<=len;i++) f(this[i],i,i==0,i==len) } //***Runs a function on each member of an array, in order; //***The function is passed the value, the index, and whether it is the first and/or last item //***e.g. var foo = [1,2,3,4,5,6,7]; //*** foo.applyToEach(function(val,i,atStart,atEnd){ //*** alert("The cube of "+val+" is "+Math.pow(val,3)); //*** if (atEnd) alert("And I'm spent"); //*** });