Monday, June 6, 2011

Javascritpt: Get Your Class Object Name

Here's the script that retrieve your Javascript object class name:

<script>
function getObjectClass(obj){
if (typeof obj != "object" || obj === null) return false;
else return /(\w+)\(/.exec(obj.constructor.toString())[1];}

function myObj(){
this.shout=function(mywords){
alert(mywords);
}
}
var x=new myObj();
x.shout('good day');

var myClassname=getObjectClass(x);
x.shout('My JS Class Name is '+ myClassname);
x.shout('My Object Constructor:' + x.constructor.toString());
</script>