How can I find all subclasses (both immediate, as well as all descendants) of a given Javascript "class"? Note that Prototype (v1.6.0) supports this with "subclasses", but I am wondering about plain Javascript.
Asked
Active
Viewed 920 times
3
-
2Javascript has no class, no style.. :( – jAndy Nov 26 '14 at 23:26
-
1You can get the **prototype** of an Object using [`Object.getPrototypeOf`](http://msdn.microsoft.com/en-us/library/ie/ff877835(v=vs.94).aspx). Is that what you're referring to? – Sampson Nov 26 '14 at 23:28
-
3Inheritance is a unidirectional relationship in JS. Unless you build something that handles your class generation and keeps track of subclasses, you can't do that (reliably). – Felix Kling Nov 26 '14 at 23:29
-
you build your own way, just like Prototype does. there's many ways to do it... – dandavis Nov 27 '14 at 00:08
1 Answers
1
JavaScript (at least ES5 and below) has no concept of classes, it only knows prototypes, which have no idea who used them as "super" or "sub" construction. The Prototype
library only knows these relations when you exclusively use its own class model on top of plain JS, simply tracking it as part of its extension API.

Mike 'Pomax' Kamermans
- 49,297
- 16
- 112
- 153
-
FWIW, ES6 doesn't have "classes" either. The `class` syntax is just syntactic sugar. – Felix Kling Nov 26 '14 at 23:31
-
-
-
For now, but with the ES6 `class` and `extends` syntax it is relatively easy for any ES6-implementing engines to also track the class relationships (at the very least because it makes JIT/shaping even better) – Mike 'Pomax' Kamermans Nov 26 '14 at 23:42
-
**2017 update**: this is no longer "sugar" but simply a much better way to write out the logic for prototype based extensible classes. – Mike 'Pomax' Kamermans Aug 02 '17 at 05:04