You don't have to do everything in jQuery. Just add the forEach method if it does not already exist and use it instead. It was so good that ECMAScript 5th ed. accepted it as a standard (inspired by the prototype of each method), but not all browsers have it yet :). Here a specification is implemented that you can use until all browsers have one ( taken from MDC ):
Change Recent versions of Chrome, Safari, Firefox, and Opera already support this. No access to IE, sorry.
if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length >>> 0; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } }; }
Then use your code as is (change each to forEach ):
var myArray = ["a","b","c"]; myArray.forEach(function(arrayEntry) { this.printPart(arrayEntry); }, this);
source share