I was playing with class/function/prototype inheritance a bit and got a decent setup working. Something simple that I understand.
http://jsfiddle.net/rudiedirkx/rwPeD/6/
For debugging purposes, I wanted to print in each constructor what kind of object was calling that constructor. For instance the Ronin constructor calls the Ninja constructor and that calls the Person constructor. For that I made a get_class
function:
function get_class(obj) {
var C = String(obj.__proto__.constructor);
return C.match(/function (\w+)\(/, C)[1];
}
and that doesn't work. It always returns "Person". Why? Every 'class' has its own constructor, doesn't it? If I do a console.log(this)
in every constructor, Chrome Devtools knows which type the object is. How do I get there (with vanilla JS)?
PS. Full output in my Chrome: