I have often used this code to determine if an array is really an array…
Object.prototype.toString.call(array) == '[object Array]'
I recently started changing my callback tests from typeof callback == 'function' to callback instanceof Function, because I read that Safari and Chrome will tell you a regex literal is a function using the former (and it did when I tested it).
Now, I decided to check this code to see if I can replace the verbose code above…
array instanceof Array
It worked.
Comparison of both on jsFiddle.
So, is there any issues with the latter method? I assumed there may be because the first code example came up much more often in Google.
The two problems with using
instanceoffor an array are:falsewhen the array is from another window or frameArrayconstructor has been overwrittenThe following is an article by kangax where I first saw your first technique (also used in jQuery): http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/