0

I just can't find out where the references to declared ES6 classes are stored, I would have expected them in the window Object, bit they don't appear there.

I don't think it's a duplicate of ES6 classes : what about instrospection? since he is asking for a existance check of a class, what I want is a list of available classes.

For example:

class Test {
    constructor() {

    }
}

window.Test // undefined

What I want is a list of all classes that extend a class of mine

To clarify that I have a structure that looks something like this:

class Base {
    constructor(components) {
        for(let component of components) {
            window[component](); // window.Test2 not defined
        }
    }

    start() {
        new this();
    }
}

class Test2 extends Base {
    constructor() {
        super();
    }
}

class Test extends Base {
    constructor() {
        super(['Test2','Test2']);
    }
}

Test.start();

That's just an abstraction of my structure, in short I have to use strings at super(['Test2', 'Test2'])

At the moment I'm doing something like this

Base.register(Test2);

for every class and I want to get rid of that.

Community
  • 1
  • 1
Dominik König
  • 124
  • 1
  • 9
  • You can't just "get a list" of all classes. You're gonna have to do something terrible, like create a global object to store all of them in. – ndugger Apr 05 '16 at 17:21
  • You could try starting with a known class, like String, and ascending the prototype chain until you find other classes. They may not all be in the same place, or all inherit from the same things. – Jon Carter Apr 05 '16 at 17:23
  • 3
    *"What I exactly want is a list of all classes that extend a class of mine"* That's impossible. – Felix Kling Apr 05 '16 at 17:27

3 Answers3

0

You can use Class expressions to store them in some sort of array, although I probably wouldn't do it, if I were you.

var Test = class Test {
   constructor() {

   }
}

allClasses.push(Test);
Luka Jacobowitz
  • 22,795
  • 5
  • 39
  • 57
0

JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.

Basically ES6 classes are compiled to plain old-fashioned Javascript functions. You may "store" them at the window object but this is a major pitfall as you are killing the whole module patter ES6 introduced.

vorillaz
  • 6,098
  • 2
  • 30
  • 46
0

If you want sort of a "module" of classes, you could theoretically do something like this:

// some-class.js

export default class SomeClass {}

Then:

// another-class.js

export default class AnotherClass {}

And your entry file:

// index.js

import SomeClass from './some-class.js' // extensions optional; here just for clarity
import AnotherClass from './another-class.js'

export default {
  SomeClass,
  AnotherClass
}

If you have all of those embedded in the same directory (we'll call the directory example), you can just import that entire module wherever you need it:

// something-that-requires-your-module.js

// this will by default enter through the `index.js` file of your `example` directory
import Example from './example';

console.log(Example.SomeClass);
console.log(Example.AnotherClass);
Josh Beam
  • 19,292
  • 3
  • 45
  • 68