JSFiddle - React, Tailwind, and code Playground
JavaScript
// ISNATIVE OBJECT 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 isNative(obj) {
switch(typeof obj) {
case 'number': case 'string': case 'boolean':
// Primitive types are not native objects
return false;
}
// 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;
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(isNative({}), '{} -- Plain object');
assert(isNative(function() {}), 'function() {} -- Plain function');
assert(isNative([]), '[] -- Plain array');
assert(isNative(/regex/), '/regex/ - Native regex');
assert(isNative(new Date()), 'new Date() - Native date object through instantiation');
assert(isNative(new String('string')), 'new String("string") - Native string object through instantiation');
assert(isNative(new Number(1)), 'new Number(1) - Native number object through instantiation');
assert(isNative(new Boolean(true)), 'new Boolean(true) - Native boolean object through instantiation');
assert(isNative(new Array()), 'new Array() - Native array object through instantiation');
assert(isNative(new Object()), '{} -- new Object() - Native object through instantiation');
assert(isNative(new Function('alert(1)')), '{} -- Native function through instantiation');
// USER OBJECT INSTANTIATION AND INHERITANCE
var Animal = function() {};
var animal = new Animal();
var Dog =...