Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created October 13, 2012 10:50
Show Gist options
  • Save mathiasverraes/3884138 to your computer and use it in GitHub Desktop.
Save mathiasverraes/3884138 to your computer and use it in GitHub Desktop.
get the class of a javascript object
var getClass = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
// define a class
function Animal() {}
// do this for all classes that need the getClass() method
Animal.prototype.getClass = getClass;
myDog = new Animal();
assert(myDog.getClass() == 'Animal');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment