B
Brett
Hi,
I'm working on a group of functions which aim to be able to sort a
supplied object. While I understand the ECMAScript spec does not
guarantee for...in iteration order, it does seem to have become a de
facto standard.
However, we encountered an issue in Explorer (and only Explorer) when
trying, within the function, to reassign the old properties with
different (i.e., sorted) values. Even when I explicitly delete all of
the properties on the object beforehand, when I reassign values to the
previously existing properties, the relative position of the keys is
remembered by Explorer (though not by Chrome, Opera, Safari, or
Firefox).
function reverseObj (obj) {
var keys = [], vals = [];
for (var p in obj) {
keys.push(p);
vals.push(obj[p]);
delete obj[p];
}
keys.reverse();
vals.reverse();
for (var i=0; i < keys.length; ++i) {
obj[keys] = vals;
}
}
var assoc = {a: 'orange', b: 'banana', c: 'apple', d: 'lemon'};
reverseObj(assoc); // Does not reverse order in IE (and IE only)
Explorer, as with the other browsers, normally does add new properties
in sequence (at the end), but it breaks this pattern for those
properties which had previously existed on the object (it reassigns
them into the same relative position as before).
Does anyone know of any way to get Explorer to truly forget those
properties ever existed on the passed-in object so the relative
positions of the properties can be freely rearranged?
I wonder if this could even be a minor security issue: if a callback
were instead given to the object after the supposed deletion of
properties, the callback could guess at the properties that had
supposedly been deleted.
While it may be the case that ECMAScript just wasn't meant to be used
in this way, I'd still like for convenience to see if it can be made
to work.
thanks,
Brett
I'm working on a group of functions which aim to be able to sort a
supplied object. While I understand the ECMAScript spec does not
guarantee for...in iteration order, it does seem to have become a de
facto standard.
However, we encountered an issue in Explorer (and only Explorer) when
trying, within the function, to reassign the old properties with
different (i.e., sorted) values. Even when I explicitly delete all of
the properties on the object beforehand, when I reassign values to the
previously existing properties, the relative position of the keys is
remembered by Explorer (though not by Chrome, Opera, Safari, or
Firefox).
function reverseObj (obj) {
var keys = [], vals = [];
for (var p in obj) {
keys.push(p);
vals.push(obj[p]);
delete obj[p];
}
keys.reverse();
vals.reverse();
for (var i=0; i < keys.length; ++i) {
obj[keys] = vals;
}
}
var assoc = {a: 'orange', b: 'banana', c: 'apple', d: 'lemon'};
reverseObj(assoc); // Does not reverse order in IE (and IE only)
Explorer, as with the other browsers, normally does add new properties
in sequence (at the end), but it breaks this pattern for those
properties which had previously existed on the object (it reassigns
them into the same relative position as before).
Does anyone know of any way to get Explorer to truly forget those
properties ever existed on the passed-in object so the relative
positions of the properties can be freely rearranged?
I wonder if this could even be a minor security issue: if a callback
were instead given to the object after the supposed deletion of
properties, the callback could guess at the properties that had
supposedly been deleted.
While it may be the case that ECMAScript just wasn't meant to be used
in this way, I'd still like for convenience to see if it can be made
to work.
thanks,
Brett