在ES6中获取超类名称 [英] Get superclass name in ES6

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

问题描述

我有一个课程,还有一个扩展该课程的课程.

I have a class, and another class that extends that class.

class Shape {
  constructor() {
    return this;
  }
}
class Circle extends Shape {
  constructor() {
    super();
    return this;
  }
}
let foo = new Circle();

我可以通过以下方式获得foo的课程

I can get foo's class with

let className = foo.constructor.name 
// returns string 'Circle'

是否可以通过类似的方式获取foo的超类("Shape")的名称?

Is it possible to get the name of foo's superclass ('Shape') in a similar manner?

推荐答案

Object.getPrototypeOf(Object.getPrototypeOf(foo)).constructor.name;

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

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