I have an object which is array-like. This means that it has numeric properties (0
, 1
, 2
...) and a length
property.
In its prototype, I declare a method to clear it, like so:
'clear': function() {
Array.prototype.splice.call(this, 0, this.length);
return this;
}
This works as expected in most browsers but not Internet Explorer.
Take a perfectly valid array-like object:
var arrayLike = {
length: 3,
'0': 'a',
'1': 'b',
'2': 'c'
};
And splice it clear:
Array.prototype.splice.call(arrayLike, 0, arrayLike.length);
On a standards compliant browser, this is the correct result:
arrayLike.length == 0 ;
arrayLike[0] == undefined;
arrayLike[1] == undefined;
arrayLike[2] == undefined;
But in IE 8, this is what you get:
arrayLike.length == 0 ; // (!!)
arrayLike[0] == 'a';
arrayLike[1] == 'b';
arrayLike[2] == 'c';
Yes, it does not even either work or not, it works as much as it wants to.
Now, I think Array.prototype.splice.call
is the only way to clear an array-like object natively. I could polyfill it in a private copy conditionally for IE8, maybe (incurring a one-time performance hit for other browsers, but only once).
Or maybe there is another way of clearing the object? That is also native in IE8? Is there something I'm not seeing?
Ps: JsFiddle for you to run in IE8.