获取 ES6 类实例的类名 [英] Get the class name of ES6 class instance

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

问题描述

是否有任何和谐"的方法可以从 ES6 类实例中获取类名?除了

Are there any 'harmonious' ways to get the class name from ES6 class instance? Other than

someClassInstance.constructor.name

目前我指望 Traceur 实现.似乎 Babel 有一个用于 Function.name 的 polyfill 而 Traceur 没有.

Currently I'm counting on Traceur implementation. And it seems that Babel has a polyfill for Function.name while Traceur doesn't.

总而言之:在 ES6/ES2015/Harmony 中没有其他方法,在 ES.Next 中没有预期的 ATM.

它可能为未缩小的服务器端应用程序提供有用的模式,但在用于浏览器/桌面/移动设备的应用程序中是不需要的.

Babel 使用 core-js 进行 polyfillFunction.name,它应该被加载为 Traceur 和 TypeScript 应用程序酌情手动.

推荐答案

someClassInstance.constructor.name 正是做到这一点的正确方法.转译器可能不支持这一点,但它是符合规范的标准方式.(通过 ClassDeclaration 产生式声明的函数的 name 属性设置在 14.5.15,第 6 步.)

someClassInstance.constructor.name is exactly the correct way to do this. Transpilers may not support this, but it is the standard way per the specification. (The name property of functions declared via ClassDeclaration productions is set in 14.5.15, step 6.)

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

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