Getting a list of statics on an ES6 class

。_饼干妹妹 提交于 2019-11-26 10:03:40

问题


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 classes 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!