最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get current 'class name' after classprototype inheritance - Stack Overflow

programmeradmin14浏览0评论

I was playing with class/function/prototype inheritance a bit and got a decent setup working. Something simple that I understand.

/

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:

I was playing with class/function/prototype inheritance a bit and got a decent setup working. Something simple that I understand.

http://jsfiddle/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:

Share Improve this question asked Jul 14, 2012 at 3:20 RudieRudie 53.9k42 gold badges135 silver badges175 bronze badges 1
  • possible duplicate of Why do you need to reset javascript constructor during inheritance? – Bergi Commented Jan 1, 2014 at 14:20
Add a ment  | 

2 Answers 2

Reset to default 6

Or you could do something that actually works...

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

obj.attr("class") only works if the method attr exists with it won't in a native environment.

Still your suggestion was very good in it's design.

Please note that proto.constructor.name will always return "Object" if the class is not a native class, such as Array, String, RegExp, Error, etc... If anyone knows how to convince the "name" property to return the real name, even if I have to add code to do it, I would buy him a banana for his/her trouble.

I think the problem refers to following line. this.prototype.constructor points to what Person.prototype points to.

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

You may need change its reference explicitly.

this.prototype.constructor = this;

The similar question can be found at Get name of derived constructor in Javascript

发布评论

评论列表(0)

  1. 暂无评论