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

问题描述

给定一个 ES6 类,我如何检查它以确定其可获取的静态属性和方法?

Given an ES6 class, how can I inspect it to determine its gettable static properties and methods?

在 ES5 中,确定附加到类(它的构造函数)的静态就像迭代函数的属性一样简单.在 ES6 中,似乎有一些魔法不会将它们暴露出来.

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.

推荐答案

是的,classes 的所有方法默认都是不可枚举的.

Yes, all methods of classes are non-enumerable by default.

您仍然可以使用 Object.getOwnPropertyNames.过滤掉 .prototype.name.length(或者只是所有不是函数的东西).要包含继承的静态方法,您必须显式遍历原型链(使用 Object.getPrototypeOf).

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-18 08:22