-
-
Save atk/1034882 to your computer and use it in GitHub Desktop.
polyfill an ES5-compatibile Array.isArray where needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.isArray||(Array.isArray=function(a){return''+a!==a&&{}.toString.call(a)=='[object Array]'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Alex Kloss <alexthkloss@web.de> | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "isarray", | |
"description": "polyfill an ES5-compatibile Array.isArray where needed.", | |
"keywords": [ | |
"polyfill", | |
"es5", | |
"isArray" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.