JSFiddle - React, Tailwind, and code Playground
JavaScript
// ISUSEROBJECT DETECTION
//
// By Steven de Salas
// http://www.desalasworks.com
//
// CC Attribution Share-Alike Licence 3.0
// http://creativecommons.org/licenses/by-sa/3.0/
function isUserObject(obj) {
// Should be an instance of an Object
if (!(obj instanceof Object)) return false;
// Should have a constructor that is an instance of Function
if (typeof obj.constructor === 'undefined') return false;
if (!(obj.constructor instanceof Function)) return false;
// Avoid built-in functions and type definitions
if (obj instanceof Function &&
Function.prototype.toString.call(obj).indexOf('[native code]') > -1)
return false;
return true;
}
// ASSERT HELPER FUNCTION
var n = 0;
function assert(condition, message) {
n++;
if (condition !== true) {
document.write(n + '. FAILS: ' + (message || '(no message)') + '<br/>');
} else {
document.write(n + '. PASS: ' + (message || '(no message)') + '<br/>');
}
}
// USER CREATED OBJECTS
assert(isUserObject({}), '{} -- Plain object');
assert(isUserObject(function() {}), 'function() {} -- Plain function');
assert(isUserObject([]), '[] -- Plain array');
assert(isUserObject(/regex/), '/regex/ - Native regex');
assert(isUserObject(new Date()), 'new Date() - Native date object through instantiation');
assert(isUserObject(new String('string')), 'new String("string") - Native string object through instantiation');
assert(isUserObject(new Number(1)), 'new Number(1) - Native number object through instantiation');
assert(isUserObject(new Boolean(true)), 'new Boolean(true) - Native boolean object through instantiation');
assert(isUserObject(new Array()), 'new Array() - Native array object through instantiation');
assert(isUserObject(new Object()), '{} -- new Object() - Native object through instantiation');
assert(isUserObject(new Function('alert(1)')), '{} -- Native function through instantiation');
// USER OBJECT INSTANTIATION AND INHERITANCE
var...