如何获取'获取'属性
How to get a list of 'get' properties?
假设我有一些对象:
Org.prototype = {
constructor : Org,
get id(){ return this._id; },
some_method: function(){},
etc...
如何检索对象的getter?
循环遍历所有属性名称,并向下筛选到属性描述符具有get
属性的名称。
function Foo() { }
Foo.prototype = {
get id() { return this._id; },
otherfunc() { }
};
function getGetters(obj) {
var proto = obj.prototype;
return Object.getOwnPropertyNames(proto)
.filter(name => Object.getOwnPropertyDescriptor(proto, name).get);
}
console.log(getGetters(Foo));
要获得具有"getter"函数的属性列表,请使用以下Object.keys
、Object.getOwnPropertyDescriptor
和Array.filter
函数的方法:
function Org(){};
Org.prototype = {
constructor : Org,
get id(){ return this._id; },
some_method: function(){}
};
propList = Object.keys(Org.prototype).filter(function (p) {
return typeof Object.getOwnPropertyDescriptor(Org.prototype, p)['get'] === "function"
});
console.log(propList);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- 从ajax请求中获取javascript对象
- Jquery,文本区域换行.don't获取 并获取↵.
- 获取Ajax获取的图像的宽度
- 当集合是惰性加载时,如何获取已获取的模型
- jQuery.获取-如何获取文本而不是html
- 如何获取'获取'属性
- PhantomJS页面使用嵌套循环获取以获取新页面
- 使用PHP获取javascript获取的值