本文介绍了获取ES6类上的静态列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在ES5中,确定附加到类的静态(它是构造函数)就像迭代函数的属性一样简单。

解决方案

是的,所有的方法 class es是默认不可枚举的。



你仍然可以使用。过滤出 .prototype .name .length 或者只是不是函数的一切)。要包括继承的静态方法,你必须明确地走原型链(使用 Object.getPrototypeOf )。


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.

解决方案

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).

这篇关于获取ES6类上的静态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:53