Skip to content

Instantly share code, notes, and snippets.

@ahuggins-nhs
Created August 3, 2020 17:04
Show Gist options
  • Save ahuggins-nhs/e61ccb900b5eb1ef3c57233a5c5e0fce to your computer and use it in GitHub Desktop.
Save ahuggins-nhs/e61ccb900b5eb1ef3c57233a5c5e0fce to your computer and use it in GitHub Desktop.
function typeOf (val) {
let typeDef = typeof val
console.log('init:', typeDef)
if (typeDef === 'object' || typeDef === 'function') {
console.log('obj/func:', typeDef)
typeDef = Object.prototype.toString.call(val)
typeDef = typeDef.substring(8, typeDef.length - 1)
}
if (typeDef === 'Object' || typeDef === 'Array') {
console.log('Obj/Arr:', typeDef)
typeDef = Object.getPrototypeOf(val).constructor.name
}
return typeDef
}
class Klass extends Array { constructor() { super() } }
class Qlass extends Klass { constructor() { super() } }
class Strang extends String { constructor() { super() } }
console.log(typeOf(null))
console.log(typeOf(async function () {}))
console.log(typeOf(function () {}))
console.log(typeOf(new Klass()))
console.log(typeOf(new Qlass()))
console.log(typeOf(new Strang()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment