在Javascript中获取派生构造函数的名称 [英] Get name of derived constructor in Javascript

查看:105
本文介绍了在Javascript中获取派生构造函数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在以下示例中获取派生类的名称?我想以某种方式使输出为ChildClass,而是它的ParentClass。

Is it possible to get the name of the derived "class" in the following example? I'd like to somehow have the output be "ChildClass", but instead it's "ParentClass".

function ParentClass() { this.name = 'Bob' }
function ChildClass() { this.name = 'Fred' }
ChildClass.prototype = Object.create(ParentClass.prototype);

var child_instance = new ChildClass()
console.log('ChildClass type:', child_instance.constructor.name)

我意识到我可以在ChildClass构造函数中执行 this.my_type ='ChildClass',但我有很多类扩展ParentClass并在任何地方执行此操作都会很不方便。

I realize I can do this.my_type = 'ChildClass' in the ChildClass constructor, but I have many classes that extend ParentClass and doing this everywhere would be inconvenient.

推荐答案

您的案例中的问题是您要覆盖 prototype ChildClass 的属性,但你没有重置构造函数属性新的原型。你需要增加一行:

The problem in your case is that you're overwriting the prototype property of ChildClass but you're not reseting the constructor property on the new prototype. You need to add one extra line:

function ParentClass() {
    this.name = "Bob";
}

function ChildClass() {
    this.name = "Fred";
}

ChildClass.prototype = Object.create(ParentClass.prototype);

ChildClass.prototype.constructor = ChildClass; // add this line to your code

现在您的代码将按预期工作。以下答案解释了原始代码无效的原因: https://stackoverflow.com/a/8096017/783743

Now your code will work as expected. The following answer explains why your original code didn't work: https://stackoverflow.com/a/8096017/783743

我个人不喜欢用构造函数和原型分别编写这样的类。打字,语无伦次,眼睛疼痛,难以维持太单调乏味。因此,我使用以下实用程序函数来创建类:

Personally I don't like writing "classes" like this with the constructor and the prototype dangling separately. It's just too tedious to type, incoherent, a pain on the eyes and difficult to maintain. Hence I use the following utility function to create classes:

function defclass(base, body) {
    var uber = base.prototype;
    var prototype = Object.create(uber);
    var constructor = (body.call(prototype, uber), prototype.constructor);
    constructor.prototype = prototype;
    return constructor;
}

现在您可以按如下方式创建课程:

Now you can create classes as follows:

var ParentClass = defclass(Object, function () {
    this.constructor = function () {
        this.name = "Bob";
    };
});

var ChildClass = defclass(ParentClass, function () {
    this.constructor = function () {
        this.name = "Fred";
    };
});

此方法有以下几个优点:

This method has several advantages:


  1. 继承和类定义已合并为一个。

  2. 构造函数只是另一种原型方法。

  3. 一切都很好封装在一个闭包中。

  4. 调用基类原型方法很简单。

  5. 您可以轻松创建私有静态函数。

  1. Inheritance and class definition have been combined into one.
  2. The constructor is just another prototype method.
  3. Everything is nicely encapsulated within a single closure.
  4. Calling base class prototype methods is easy.
  5. You can create private static functions easily.

希望有所帮助。

这篇关于在Javascript中获取派生构造函数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆