A
Aaron Gray
I know this has probably been argued to death, but I am going to raise it
fresh again, and basically lets have an unofficial 'isArray()' contest that
we can hopefully put it to rest as best as we can.
I have found things that work perfectly well as long as you don't try them
on MSIE, ie adding Object and Array prototype isArray functions, thus :-
Object.prototype.isArray = function() { return false }
Array.prototype.isArray = function() { return true }
This falls down on builtin browser types in IE, so is no good.
The one version that I have seen is :-
function isArray( a) { return typeof a.push == "function" }
Prototype for all its "failings" extends object and provides :-
Object.extend(Object, {
isArray: function(object) {
return object != null && typeof object == "object" &&
'splice' in object && 'join' in object;
}
});
So checking of non null and splice and join maybe better than just checking
for push.
So something like :-
function isArray( o) { return o != null && typeof o == "object" &&
'push' in o }
But I am not sure when 'in' was actually introduced.
function isArray( o) { return o != null && typeof o == "object" &&
typeof o.push == "function" }
Would probably do best/better. This is what I have settled on for now.
Then there was the advice to use === rather than ==, but I dont know when
that was introduced too, or whether it is really necessary as == works just
as well AFAICS.
Any critisisms, advances, or advice ?
Aaron
fresh again, and basically lets have an unofficial 'isArray()' contest that
we can hopefully put it to rest as best as we can.
I have found things that work perfectly well as long as you don't try them
on MSIE, ie adding Object and Array prototype isArray functions, thus :-
Object.prototype.isArray = function() { return false }
Array.prototype.isArray = function() { return true }
This falls down on builtin browser types in IE, so is no good.
The one version that I have seen is :-
function isArray( a) { return typeof a.push == "function" }
Prototype for all its "failings" extends object and provides :-
Object.extend(Object, {
isArray: function(object) {
return object != null && typeof object == "object" &&
'splice' in object && 'join' in object;
}
});
So checking of non null and splice and join maybe better than just checking
for push.
So something like :-
function isArray( o) { return o != null && typeof o == "object" &&
'push' in o }
But I am not sure when 'in' was actually introduced.
function isArray( o) { return o != null && typeof o == "object" &&
typeof o.push == "function" }
Would probably do best/better. This is what I have settled on for now.
Then there was the advice to use === rather than ==, but I dont know when
that was introduced too, or whether it is really necessary as == works just
as well AFAICS.
Any critisisms, advances, or advice ?
Aaron