TypeScript / JavaScript:如何扫描所有全局类(并按超类过滤)?

时间:2022-10-30 00:18:25

1) In TypeScript, how can I find all classes defined at global level?
2) How can I filter them by super class?

1)在TypeScript中,如何找到在全局级别定义的所有类? 2)我如何通过超级过滤它们?

I have model classes, which extend FrameModel. These are not known before runtime (pluggable).
So I want to get a list of subclasses of FrameModel.

我有扩展FrameModel的模型类。这些在运行时(可插入)之前是未知的。所以我想得到一个FrameModel的子类列表。

This is my attempt:

这是我的尝试:

public static scan()
{
    this.mapping = {};
    for (var i = 0; i < props.length; i++) {
        var key = props[i];
        var val = window[key];
        console.log(`Key: ${key} Val: ${val} ${typeof val}`);
        if (val == null)
            continue;
        if (typeof val !== 'function')
            continue;
        if (val.prototype == void 0)
            continue;
        if (val.prototype.constructor !== FrameModel)
            continue;
        this.mapping[key] == val; // Doesn't work
    }
}

What I consider problematic is:

我认为有问题的是:

  1. The use of window - let's say it won't run in the browser. Then I need a global object in general. I've seen using this in some context, IIRC in a function which is called outside an object, to get the global object. Is that the "official" way?

    窗口的使用 - 假设它不会在浏览器中运行。然后我需要一个全局对象。我已经看到在某些上下文中使用它,IIRC在一个被称为对象外部的函数中,以获取全局对象。这是“官方”的方式吗?

  2. window[key] doesn't work - Window is not assignable to typeof FrameModel

    window [key]不起作用 - Window不能分配给typeof FrameModel

  3. It scans all props of Window, not just Functions. I assume there's some smarter way to get a list of Functions.

    它会扫描Window的所有道具,而不仅仅是函数。我假设有一些更聪明的方法来获取函数列表。

TypeScript 1.8. Playground

TypeScript 1.8。操场

1 个解决方案

#1


0  

These are not known before runtime (pluggable).

这些在运行时(可插入)之前是未知的。

Magical introspection does not work well with JavaScript. You generally want some dependency framework (e.g the one that comes with angular driven by explicit code i.e. decorators)

神奇的内省对JavaScript不起作用。您通常需要一些依赖框架(例如由显式代码即装饰器驱动的角度引入的框架)

I recommend looking into https://github.com/inversify/InversifyJS as the DI framework of choice.

我建议将https://github.com/inversify/InversifyJS作为首选的DI框架。

More

Just for your curiosity:

只是为了你的好奇心:

The use of window - let's say it won't run in the browser. Then I need a global object in general

窗口的使用 - 假设它不会在浏览器中运行。然后我需要一个全局对象

If you use global webpack will automatically point it to window when bundling e.g. : console.log(global); // Window. Ofcourse in NodeJs it would be the global: https://nodejs.org/api/globals.html#globals_global

如果您使用全局webpack将捆绑时自动将其指向窗口,例如:console.log(global); //窗口NodeJs中的当然是全球性的:https://nodejs.org/api/globals.html#globals_global

Setting up webpack for typescript : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

为打字稿设置webpack:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

#1


0  

These are not known before runtime (pluggable).

这些在运行时(可插入)之前是未知的。

Magical introspection does not work well with JavaScript. You generally want some dependency framework (e.g the one that comes with angular driven by explicit code i.e. decorators)

神奇的内省对JavaScript不起作用。您通常需要一些依赖框架(例如由显式代码即装饰器驱动的角度引入的框架)

I recommend looking into https://github.com/inversify/InversifyJS as the DI framework of choice.

我建议将https://github.com/inversify/InversifyJS作为首选的DI框架。

More

Just for your curiosity:

只是为了你的好奇心:

The use of window - let's say it won't run in the browser. Then I need a global object in general

窗口的使用 - 假设它不会在浏览器中运行。然后我需要一个全局对象

If you use global webpack will automatically point it to window when bundling e.g. : console.log(global); // Window. Ofcourse in NodeJs it would be the global: https://nodejs.org/api/globals.html#globals_global

如果您使用全局webpack将捆绑时自动将其指向窗口,例如:console.log(global); //窗口NodeJs中的当然是全球性的:https://nodejs.org/api/globals.html#globals_global

Setting up webpack for typescript : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

为打字稿设置webpack:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html