【新提案】增加运行时获取class field的手段 #33
Unanswered
LongTengDao
asked this question in
Ideas
Replies: 2 comments 5 replies
-
What's the use case where you need to know what class fields a class has? In this example: class X {
a;
constructor() {
Object.defineProperty(this, 'b', {});
}
} Why do you need a list that contains "a" but doesn't also contain "b"? |
Beta Was this translation helpful? Give feedback.
4 replies
-
首先你没法保证开发者是否使用了 babel 等编译工具,一旦使用了,这个能力就会完全丢失。 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
目前,不parse ast是无法获知
class A { a; b; }
中field的列表的。运行时它的开销很大,而且也无法解析结果(如果field是一个计算属性)。这限制了框架设计的能力。方案1:
Function.getClassFields(class{a;}) //['a']
方案2:
(class{a;})[Symbol.classFields] //['a']
方案3:
Reflect.getClassFields(class{a;}) //['a']
个人认为,第一个方案似乎比较好
Beta Was this translation helpful? Give feedback.
All reactions