For me, the $.each
utility can generally be replaced with the for..in
statement:
$.each (object, function () {
// do something with this
}
vs
var current;
for (key in object) {
current = object[key];
// do something with current
}
The only additional feature $.each
has is that it creates a new scope, allowing you to have more localised variables:
var someVar = 'original string';
$.each (object, function () {
// do something with this
var someVar = 'new string';
}
console.log(someVar); // 'original string'
You can, however, replicate this using for..in
using self-executing functions:
var someVar = 'original string';
for (key in object) {
(function() {
var current = object[key],
someVar = 'new string';
})();
}
console.log(someVar); // 'original string'