JSFiddle - React, Tailwind, and code Playground
by Jeremy Banks
HTML
<iframe id="iframe" src="/echo/html/" style="display: none;"></iframe>
JavaScript
/*
http://www.shamasis.net/2011/08/infinite-ways-to-detect-array-in-javascript
http://bytes.com/topic/javascript/answers/577118-robust-isarray
Ah, previously discovered here:
http://mobzish.blogspot.ca/2008/10/bulletproof-isarray-for-javascript.html
*/
var RealArray = Array;
var FakeArray = (function() {
function Array() {
this.length = 0;
};
return Array;
}());
var SortOfFakeArray = (function() {
function Array() {
this.length = 0;
};
Array.prototype.constructor = RealArray;
return Array;
}());
var ForeignArray = document.querySelector("#iframe").contentWindow.Array;
function isArray(x) {
return [].concat(x)[0] !== x;
}
function dumbIsArray(x) {
return Object.prototype.toString.call(x) === "[object Array]";
}
function reprOfArray(x) {
return Object.prototype.toString.call(x);
}
console.log("-- This check:");
console.log(isArray(new Array));
console.log(isArray(new ForeignArray));
console.log(isArray(new FakeArray));
console.log(isArray(new SortOfFakeArray));
console.log("-- Old check:");
console.log(dumbIsArray(new Array));
console.log(dumbIsArray(new ForeignArray));
console.log(dumbIsArray(new FakeArray));
console.log(dumbIsArray(new SortOfFakeArray));
console.log("-- String reprs:");
console.log(reprOfArray(new Array));
console.log(reprOfArray(new ForeignArray));
console.log(reprOfArray(new FakeArray));
console.log(reprOfArray(new SortOfFakeArray));