Maybe my example was misleading by using an `import`. What I'm talking about is 
inspecting the module from within itself. Similar to `this` or `self`,  AFAIK, 
I can't get a reference to the module from within the module - e.g., `module`. 
Say, I'm in a module that has a default export of class "Foo". In a function 
*in* that module (or class), I want to create an instance of that default 
export. I can do it by name `new Foo()`. But, in more generic code, I need to 
do it by "role" - `default`. So my generic version wants something that 
represents `new module.default()`. This is because I may not have access to the 
name - say the function is in a base class. Take for example a "type" module, 
that is a wrapper for a single class, and the filename = class name. One way I 
can implement `instanceof` is a string approach:

```
    // Foo.mjs
    export default class Foo {
        static type = import.meta.url;

        meta = { type: import.meta.url };

        // instanceof
        static [Symbol.hasInstance](instance) {
            return instance.meta.type === Foo.type;
        }
    }
```
Some weaknesses are it's strings, and I need to know the name of the class (for 
`Foo.type`). So I can't move this to a base class.

With module reflection, I could do this with objects:
```
    // Foo.mjs
    export default class Foo {
        meta = { type: import.reflect.default };

        // instanceof
        static [Symbol.hasInstance](instance) {
            return instance.meta.type === import.reflect.default;
        }
    }
```

A similar idea would be to define a Module Scope - `module`. Within a module, 
using `module` would be like a "static self" - it works on the structure (e.g., 
definition) of the module.
Then `import.reflect.default`  could be `module.default`.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to