假设我有这个类(我使用它像enum):
class Color { static get Red() { return 0; } static get Black() { return 1; } }
有没有类似于Object.keys来获得[‘Red’,’Black’]?
我正在使用Node.js v6.5.0,这意味着某些功能可能会丢失.
解决方法
使用
Object.getOwnPropertyDescriptors()
并过滤结果以仅包含具有getter的属性:
class Color { static get Red() { return 0; } static get Black() { return 1; } } const getters = Object.entries(Object.getownPropertyDescriptors(Color)) .filter(([key,descriptor]) => typeof descriptor.get === 'function') .map(([key]) => key) console.log(getters)
您也可以尝试这种方法 – 它应该在Node.js 6.5.0中工作.
class Color { static get Red() { return 0; } static get Black() { return 1; } } const getters = Object.getownPropertyNames(Color) .map(key => [key,Object.getownPropertyDescriptor(Color,key)]) .filter(([key,descriptor]) => typeof descriptor.get === 'function') .map(([key]) => key) console.log(getters)
原文地址:https://www.jb51.cc/js/157775.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。