Skip to content

Instantly share code, notes, and snippets.

@danny-andrews
Created July 11, 2017 19:12
Show Gist options
  • Save danny-andrews/71409780442d4930b1a0c5e27f850208 to your computer and use it in GitHub Desktop.
Save danny-andrews/71409780442d4930b1a0c5e27f850208 to your computer and use it in GitHub Desktop.
Get Class Javascript
/**
* Returns internal [[Class]] property of an object
*
* Ecma-262, 15.2.4.2
* Object.prototype.toString( )
*
* When the toString method is called, the following steps are taken:
* 1. Get the [[Class]] property of this object.
* 2. Compute a string value by concatenating the three strings "[object ", Result (1), and "]".
* 3. Return Result (2).
*
* __getClass(5); // => "Number"
* __getClass({}); // => "Object"
* __getClass(/foo/); // => "RegExp"
* __getClass(''); // => "String"
* __getClass(true); // => "Boolean"
* __getClass([]); // => "Array"
* __getClass(undefined); // => "Window"
* __getClass(Element); // => "Constructor"
*
*/
const getClass = object => Object.prototype
.toString
.call(object)
.match(/^\[object\s(.*)\]$/)[1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment