问题描述
在阅读ecmascript-harmony规范时,我无法看到有关检查实例/对象的任何信息,以查找有关其类和模块的信息。我想要做的是能够检查一个javascript对象(这是一个es6和谐类的实例)并找出:
When reading the ecmascript-harmony specification I cannot see anything about inspecting instances/objects in regards to find out information about their class and module. What I want to do is to be able to inspect a javascript object (that is an instance of a es6 harmony class) and find out:
- 从中实例化的类的名称
- 在类中定义的模块的名称
- 可能的超类的名称及其模块
有人知道这是否可以在es6中使用?
Does anybody know if this will be possible in es6?
如果没有,是不是可能或首选的原因?
If not is there a reason it would not be possible or preferred?
我可以想到一个可能的问题,一个类没有与它们的模块绑定,因为类是用其他语言的包,即Java。即如果在ModuleA中定义ClassA并且ModuleB导入ClassA以便以后重新导出它会发生什么?
I can think of a possible issue with a classes not being as "tied" to their module as classes are to packages in other languages, i.e. Java. I.e. what would happen if ClassA is defined in ModuleA and ModuleB imports ClassA to later re-export it?
推荐答案
是的。您可以在ES6中访问 .constructor.name
。
Yes. You can access .constructor.name
in ES6.
您可以通过原型链访问超级类。
Yes. You can access super classes via the prototype chain.
不,这是不可能的。调试器可能能够找到定义类的源文件,如果引擎支持,但代码不支持。模块和类并不像你想象的那样静态,它们肯定没有名称空间,所以无论如何这都是无用的。
No, that's not possible. A debugger might be able to locate the source file a class was defined in, if the engine supports that, but code does not. Modules and classes are not as static as you might think, and they're certainly no namespaces, so this is useless anyway.
这篇关于是否有可能在ecmascript-harmony中找到对象的类和模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!