-
-
Save atk/1034882 to your computer and use it in GitHub Desktop.
| // only set function if not already available | |
| Array.isArray || (Array.isArray = function( | |
| a // array or not array, this is the question | |
| ){ | |
| return | |
| // is not the string '[object Array]' and | |
| '' + a !== a && | |
| // test with Object.prototype.toString | |
| {}.toString.call(a) == '[object Array]' | |
| }); |
| Array.isArray||(Array.isArray=function(a){return''+a!==a&&{}.toString.call(a)=='[object Array]'}); |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| Version 2, December 2004 | |
| Copyright (C) 2011 Alex Kloss <[email protected]> | |
| Everyone is permitted to copy and distribute verbatim or modified | |
| copies of this license document, and changing it is allowed as long | |
| as the name is changed. | |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
| 0. You just DO WHAT THE FUCK YOU WANT TO. |
| { | |
| "name": "isarray", | |
| "description": "polyfill an ES5-compatibile Array.isArray where needed.", | |
| "keywords": [ | |
| "polyfill", | |
| "es5", | |
| "isArray" | |
| ] | |
| } |
| <!DOCTYPE html> | |
| <title>Foo</title> | |
| <div>Expected value: <b>false, true, false, false, false, false</b></div> | |
| <div>Actual value: <b id="ret"></b></div> | |
| <script> | |
| // write a small example that shows off the API for your example | |
| // and tests it in one fell swoop. | |
| var isArray = Array.isArray||(Array.isArray=function(a){return''+a!==a{}.toString.call(a)=='[object Array]'}); | |
| document.getElementById( "ret" ).innerHTML = [isArray({}), isArray([]), isArray(""), isArray(0), isArray(true), isArray('[object Array]')]; | |
| </script> |
@subzey : ok, thanks :)
Not even that, what if I want to test a string containing "Array" at any point. Granted, if you try a string containing "[object Array]" it will return a false positive, too, yet this is less problematic, so if you want to be sure, add &&''+a!==a.
Maybe we could use the behavior of Array.prototype.concat so interpreter natively checks if argument is an array
Array.isArray?0:Array.isArray=function(a){return[].concat(a)!==a}@subzey
(function(a){return[].concat(a)!==a})('') // => true in firefox 10
Thanks, @atk!
Sorry, I missed [0]. That what I meant was
function(a){return[].concat(a)[0]!==a}Array.prototype.concat adds argument's contents to this array if arg is an array and otherwise it adds the argument itself.
I wanted to exploit this difference, but lately I've found an error: it won't work correctly if argument is an array and its 0'th value is the same object (var a = []; a[0] = a).
Ah, that explains it.
Added another fix. If you tried to test the format of '[object Array]', it would give you a false positive; by ensuring that we haven't got a string, this is now fixed, too.
@sebastien-p, It may conflict with typed arrays.