To check if an element is an array in JavaScript, I always used the Crockford function (page 61 of โThe Good Partsโ):
var is_array = function (value) { return value && typeof value === 'object' && typeof value.length === 'number' && typeof value.splice === 'function' && !(value.propertyIsEnumerable('length')); }
But if I'm not mistaken, recently some guy from Google found a new way to test the JavaScript array, but I just canโt remember where I read it from and how the function went.
Can someone please point me to his decision please?
[Update]
The person from Google who apparently discovered this is called Mark Miller .
Now I also read that from this post that his solution can also easily break:
// native prototype overloaded, some js libraries extends them Object.prototype.toString= function(){ return '[object Array]'; } function isArray ( obj ) { return Object.prototype.toString.call(obj) === '[object Array]'; } var a = {}; alert(isArray(a)); // returns true, expecting false;
So, I ask, is there a way to verify the validity of an array?
javascript arrays
Andreas Grech
source share