Skip to content

Instantly share code, notes, and snippets.

@RinatValiullov
Last active January 21, 2018 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RinatValiullov/4c3f6ecceb6aecc06a6ed8447ed152c3 to your computer and use it in GitHub Desktop.
Save RinatValiullov/4c3f6ecceb6aecc06a6ed8447ed152c3 to your computer and use it in GitHub Desktop.
Get an attribute "class" of any object
function getClassOfObject( object ) {
if ( object === null ) return "Null";
if ( object === undefined ) return "Undefined";
return Object.prototype.toString.call( object ).slice(8, -1);
};
getClassOfObject(null) // "Null"
getClassOfObject('string') // "String"
getClassOfObject(111) // "Number"
getClassOfObject(Object.constructor) // "Function"
getClassOfObject(Object.prototype) // "Object"
getClassOfObject(Object) // "Function"
getClassOfObject(null) // "Null"
getClassOfObject(Symbol) // "Function"
getClassOfObject(window) // "Window"
getClassOfObject(/./) // "RegExp"
function foo(){}
getClassOfObject(new foo()) // "Object"
getClassOfObject([]) // "Array"
getClassOfObject({}) // "Object"
getClassOfObject() // "Undefined"
getClassOfObject(getClassOfObject) // "Function"
getClassOfObject(true) // "Boolean"
getClassOfObject(5 === 5) // "Boolean"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment