Created
September 19, 2011 11:37
-
-
Save melvynhills/1226337 to your computer and use it in GitHub Desktop.
JavaScript getClass function
This file contains hidden or 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 getClass(object) { | |
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1].toLowerCase(); | |
} | |
var o = {'hello':'world'}; | |
console.log(typeof o, getClass(o)); // object, object | |
var a = ['hello','world']; | |
console.log(typeof a, getClass(a)); // object, array | |
var d = new Date("October 13, 1975 11:13:00"); | |
console.log(typeof d, getClass(d)); // object, date | |
var n = 56788; | |
console.log(typeof n, getClass(n)); // number, number | |
var s = "hello world"; | |
console.log(typeof s, getClass(s)); // string, string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full explanation at http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/