获取ES6类的静态列表

获取ES6类的静态列表

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

问题描述

给定一个ES6类,我如何检查它来确定其gettable静态属性和方法?



在ES5中确定附加到类的静态(它是构造函数)就像迭代函数的属性一样简单。在ES6中,似乎有一些魔法不会让他们如此。

解决方案

是的,所有的方法



您还可以使用 Object.getOwnPropertyNames 。过滤掉 .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