Last active
January 21, 2018 19:12
-
-
Save RinatValiullov/4c3f6ecceb6aecc06a6ed8447ed152c3 to your computer and use it in GitHub Desktop.
Get an attribute "class" of any object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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