获取ES6类实例的类名 [英] Get the class name of ES6 class instance
问题描述
someClassInstance.constructor.name
目前我正在依靠Traceur实现。而且,Babel似乎有一个polyfill for Function.name
而Traceur没有。
总结一下:ES6 / ES2015 / Harmony中没有其他方法,ES.Next中没有预期的ATM。
它可能为未破坏的服务器端应用程序提供有用的模式,但在用于浏览器/桌面/移动设备的应用程序中不需要。
Babel 使用 core-js
进行polyfill Function.name
,应该是
someClassInstance.constructor.name
正是这样做的正确方法。透平机可能不支持这一点,但它是规范的标准方法。 (通过ClassDeclaration生产声明的函数的名称属性设置在 14.5.15 ,步骤6)。
Are there any 'harmonious' ways to get the class name from ES6 class instance? Other than
someClassInstance.constructor.name
Currently I'm counting on Traceur implementation. And it seems that Babel has a polyfill for Function.name
while Traceur doesn't.
To sum it all up: there was no other way in ES6/ES2015/Harmony, and nothing is expected ATM in ES.Next.
It may provide useful patterns for unminified server-side applications but is unwanted in applications meant for browser/desktop/mobile.
Babel uses core-js
to polyfill Function.name
, it should be loaded manually for Traceur and TypeScript applications as appropriate.
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屋!