问题
Given an ES6 class, how can I inspect it to determine its gettable static properties and methods?
In ES5 determining the statics attached to a class (it\'s constructor) was as simple as iterating over the properties of the function. In ES6, is appears there is some magic going on that doesn\'t expose them as such.
回答1:
Yes, all methods of class
es are non-enumerable by default.
You still can iterate them using Object.getOwnPropertyNames. Filter out .prototype
, .name
and .length
(or just everything that is not a function). To include inherited static methods, you will have to walk the prototype chain explicitly (using Object.getPrototypeOf
).
来源:https://stackoverflow.com/questions/33069692/getting-a-list-of-statics-on-an-es6-class