adding weepy's suggestion to use (for .. in) for array comprehensions, which means that they're now object comprehensions as well

This commit is contained in:
Jeremy Ashkenas
2009-12-31 14:52:14 -05:00
parent c8e820e851
commit 3042a50f87
3 changed files with 48 additions and 43 deletions

View File

@@ -3,28 +3,31 @@
// The cornerstone, an each implementation.
// Handles objects implementing forEach, arrays, and raw objects.
_.each = function each(obj, iterator, context) {
var __a, __b, __c, __d, __e, __f, __g, __h, i, index, item, key;
var __a, __b, __c, __d, __e, i, index, item, key;
index = 0;
try {
if (obj.forEach) {
obj.forEach(iterator, context);
} else if (_.isArray(obj) || _.isArguments(obj)) {
__a = obj;
__d = [];
for (__b=0, __c=__a.length; __b<__c; __b++) {
item = __a[__b];
i = __b;
__d[__b] = iterator.call(context, item, i, obj);
__b = [];
for (i in __a) {
if (__a.hasOwnProperty(i)) {
item = __a[i];
__b[i] = iterator.call(context, item, i, obj);
}
}
__d;
__b;
} else {
__e = _.keys(obj);
__h = [];
for (__f=0, __g=__e.length; __f<__g; __f++) {
key = __e[__f];
__h[__f] = iterator.call(context, obj[key], key, obj);
__c = _.keys(obj);
__e = [];
for (__d in __c) {
if (__c.hasOwnProperty(__d)) {
key = __c[__d];
__e[__d] = iterator.call(context, obj[key], key, obj);
}
}
__h;
__e;
}
} catch (e) {
if (e !== breaker) {

View File

@@ -2,28 +2,31 @@
// The cornerstone, an each implementation.
// Handles objects implementing forEach, arrays, and raw objects.
_.each = function each(obj, iterator, context) {
var __a, __b, __c, __d, __e, __f, __g, __h, i, index, item, key;
var __a, __b, __c, __d, __e, i, index, item, key;
index = 0;
try {
if (obj.forEach) {
obj.forEach(iterator, context);
} else if (_.isArray(obj) || _.isArguments(obj)) {
__a = obj;
__d = [];
for (__b=0, __c=__a.length; __b<__c; __b++) {
item = __a[__b];
i = __b;
__d[__b] = iterator.call(context, item, i, obj);
__b = [];
for (i in __a) {
if (__a.hasOwnProperty(i)) {
item = __a[i];
__b[i] = iterator.call(context, item, i, obj);
}
}
__d;
__b;
} else {
__e = _.keys(obj);
__h = [];
for (__f=0, __g=__e.length; __f<__g; __f++) {
key = __e[__f];
__h[__f] = iterator.call(context, obj[key], key, obj);
__c = _.keys(obj);
__e = [];
for (__d in __c) {
if (__c.hasOwnProperty(__d)) {
key = __c[__d];
__e[__d] = iterator.call(context, obj[key], key, obj);
}
}
__h;
__e;
}
} catch (e) {
if (e !== breaker) {