Get current "class name" after class / prototype inheritance

I played a little with class / function / prototype inheritance and worked on a decent setup. Something simple that I understand.

http://jsfiddle.net/rudiedirkx/rwPeD/6/

For debugging purposes, I wanted to print in each constructor which object this constructor called. For example, the Ronin constructor calls the Ninja constructor and calls the Person constructor. For this, I made a function get_class:

function get_class(obj) {
    var C = String(obj.__proto__.constructor);
    return C.match(/function (\w+)\(/, C)[1];
}

and it won’t work. He always returns "Face." What for? Each "class" has its own constructor, right? If I do console.log(this)in every constructor, Chrome Devtools knows what type of object is. How do I get (with vanilla JS)?

PS. Full output in my Chrome:

Chrome Devtools output

+5
2

-, ...

function getClass(obj) {
    return obj.__proto__.constructor.name;
}

obj.attr( "class" ) , attr .

, .

, proto.constructor.name "Object", , Array, String, RegExp, Error .. - "" , , , / .

+3

, . this.prototype.constructor , Person.prototype.

this.prototype = Object.create(sup.prototype);

.

this.prototype.constructor = this;

Javascript

0

All Articles