Last active
August 29, 2015 14:08
-
-
Save shamasis/11c3da8eff4a2d7a6349 to your computer and use it in GitHub Desktop.
Best JavaScript Array Detection Technique
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
/** | |
* Check whether an object is Array or not | |
* @type Boolean | |
* @param {object} subject is the variable that is | |
* tested for Array identity check | |
*/ | |
var isArray = (function () { | |
// Use browser's own `isArray` when available | |
if (Array.isArray) { | |
return Array.isArray; | |
} | |
// Retain references to variables for performance optimization | |
var objToStringFn = Object.prototype.toString, | |
arrToStringResult = objToStringFn.call([]); | |
// Use object prototype's toString method as comparison | |
return function (subject) { | |
return objToStringFn.call(subject) === arrToStringResult; | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment