Created
July 11, 2017 19:12
-
-
Save danny-andrews/71409780442d4930b1a0c5e27f850208 to your computer and use it in GitHub Desktop.
Get Class Javascript
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
/** | |
* 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