admin管理员组文章数量:1295722
How to get list of methods defined for a class in js?
class c {
methodA(){}
static methodB(){}
log(){console.log(/*methods*/);}
static logStatic(){console.log(/*static methods*/)}
}
Thanks
How to get list of methods defined for a class in js?
class c {
methodA(){}
static methodB(){}
log(){console.log(/*methods*/);}
static logStatic(){console.log(/*static methods*/)}
}
Thanks
Share asked Aug 24, 2018 at 18:52 Márius RakMárius Rak 1,4722 gold badges18 silver badges39 bronze badges 01 Answer
Reset to default 10You can use Object.getOwnPropertyNames
and filter the instance
and static
methods:
class c {
methodA(){}
static methodB(){}
log(){console.log(/*methods*/);}
static logStatic(){console.log(/*static methods*/)}
}
const instanceOnly = Object.getOwnPropertyNames(c.prototype)
.filter(prop => prop != "constructor");
console.log(instanceOnly);
const staticOnly = Object.getOwnPropertyNames(c)
.filter(prop => typeof c[prop] === "function");
console.log(staticOnly);
本文标签: Get methods of class in JavaScriptStack Overflow
版权声明:本文标题:Get methods of class in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741622279a2388879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论