PDA

View Full Version : [NOREPRO] Ext.each() params are backwards



sissonb
1 Apr 2013, 1:11 PM
The iterate and each functions have the parameters backwards.

Ext.each(array, function(value, key)){});
Ext.each(array, function(key,value)){});

Ext.iterate(object, function(value, key)){});
Ext.iterate(object, function(key,value)){});

This applies to all versions of Touch and ExtJS.

mitchellsimoens
2 Apr 2013, 5:27 AM
With this test case:


var arr = [
'foo',
'bar'
];

Ext.each(arr, function(value, index) {
console.log(value, index);
});

var obj = {
foo : 'bar',
bar : 'meh'
};

Ext.iterate(obj, function(key, value) {
console.log(key, value);
});

I do not see the issue. I get this in my console:

foo 0
bar 1
foo bar
bar meh

which to me is 100% correct according to the documentation.

eric.cook
2 Apr 2013, 1:10 PM
I think the confusion here is caused by the function arguments for Ext.Array.each and Ext.Object.each not being consistent with each other. Functionally they work exactly as they are supposed to, but you can't use the same method for both and have it work correctly.



Ext.Array.each(anArray,function(arg1 /* value */, arg2 /* index */, array){
// array[arg2] === arg1
});

Ext.Object.each(anObject, function(arg1 /* key */, arg2 /* value */, obj){
// obj[arg1] === arg2
});


It would make more sense if the first two arguments for Ext.Object.each were changed to (value, key) but it's a moot point anyway since it's not going to change.

mitchellsimoens
2 Apr 2013, 1:11 PM
I can understand the argument confusion but the difference is a logical one between an array and object IMO.

eric.cook
2 Apr 2013, 1:26 PM
There is a logical difference between arrays and objects, true. But consider the following:



var myArray = [1, 2, 3];
var myObject = {
"0": 1,
"1": 2,
"2": 3,
length: 3
};


In the current model, I can't iterate over these objects with the same method. Although I could do some cool JavaScript magic and come up with something like this:



MyApp.makeIterator = function(fn){
return function(value, index, obj){
if(Ext.isArray(obj)) fn.call(this, value, index, obj);
else fn.call(this, index, value, obj);
};
};


This may not be completely correct but the point is that I can reuse my iterator now without having to worry about what kind of object I'm working on.

Just an idea to play with.

sissonb
2 Apr 2013, 2:07 PM
Good ideas Eric. Maybe a function like, MyApp.makeIterator, could make its way into Sencha and the "Ext.Array.each" and "Ext.Object.each" could be depreciated.

Check out how jQuery implemented this function http://api.jquery.com/jQuery.each/