最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - How to get list of all static members of some class in javascript - Stack Overflow

programmeradmin4浏览0评论

I would like to get list of all static members of some class. For example: I would like to get all static members of Object (like Object.create if avalible and so on). How can I do that?

Example:

var ClassA = function(){}
ClassA.prototype.getName = function(){return "ClassA";} //public method
ClassA.alertName = function(){ alert("ClassA");} //static method
ClassA.doSomething = function(){return "Do something";} //another static method

So, if I got more static members, I would like to get at least names of them. In this example I would like to get alertName and doSomething. With public members you can do something like that:

for (i in ClassA.prototype) {
    alert(i);
}

How about with static members?

I would like to get list of all static members of some class. For example: I would like to get all static members of Object (like Object.create if avalible and so on). How can I do that?

Example:

var ClassA = function(){}
ClassA.prototype.getName = function(){return "ClassA";} //public method
ClassA.alertName = function(){ alert("ClassA");} //static method
ClassA.doSomething = function(){return "Do something";} //another static method

So, if I got more static members, I would like to get at least names of them. In this example I would like to get alertName and doSomething. With public members you can do something like that:

for (i in ClassA.prototype) {
    alert(i);
}

How about with static members?

Share Improve this question edited Mar 13, 2013 at 16:48 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Mar 13, 2013 at 15:59 AlFraAlFra 4191 gold badge7 silver badges17 bronze badges 9
  • 1 Try developer.mozilla/en-US/docs/JavaScript/Reference/… or developer.mozilla/en-US/docs/JavaScript/Reference/… (depending on what you want) – Ian Commented Mar 13, 2013 at 16:04
  • sorry for that. You can get public members with something like this: for(var i in obj){alert(i + " : " + obj[i]);} but it is different with classes. I will check your suggestion, but I see problem with browser patibilities (supports only ie9 for example) – AlFra Commented Mar 13, 2013 at 16:16
  • So try to explain more what you mean by "static members". What do you mean "but it is different with classes"? Object.keys has a polyfill that you can insert to include patibility for older browsers (scroll down to the "Compatibility" section). The Object.getOwnPropertyNames is a little different, so I'm not sure if it's what you're looking for anyways. – Ian Commented Mar 13, 2013 at 16:23
  • Just to be sure, you're trying to get a list of all static members of some class? – Vivin Paliath Commented Mar 13, 2013 at 16:28
  • 1 @AleksanderFras: Wele to Stack Overflow! Please make a note of the formatting improvements that have been made to your post; documentation on the formatting options on Stack Overflow can be found at stackoverflow./editing-help – Matt Commented Mar 13, 2013 at 16:50
 |  Show 4 more ments

2 Answers 2

Reset to default 12

How are you trying to inspect them?

Why can't you just use the same iterator to go through your actual class?

var key = "";
for (key in MyClass) { console.log(key); }

This isn't your every-day language.
Practically everything in JS is an object, including functions/constructor functions.

As such, what seems like "public static" to you, here, is actually just a method of an object, which can be iterated through, like any other object.

Also: prototyping IS public static.
If you prototype a property into your instances, then all instances have a reference to that exact same property, and modifications of that property will change the reference for everyone else.

You can use Object.keys

Object.keys(MyClass);

This will give you every static property of whatever you want. Also, you don't need to check for inherited properties.

发布评论

评论列表(0)

  1. 暂无评论