简体   繁体   中英

ES6 - Exporting module with a getter

would like to export a module that get's the module's definition from some global object.

It's something like:

export {
  get DynamicModule() {
    return __globalFluxStorage.state.property.property.property.property
  }
}

...

import {DynamicModule} from 'dynamic-module'

We have a complex flux storage and DynamicModule is just a means of accessing __globalFluxStorage.state.property.property.property.property without the need to type in the long property accessor. Is this possible? Thanks.

Edit:

Since I am using babel, tried something like this:

Object.defineProperty(module.exports, "Forms", {
  get: function() {
    return __globalFluxStorage.state.property.property.property.property
  }
});

But does not work, ie {DynamicModule} is undefined

No, it is impossible to make a getter for a module export - they are variable bindings, not properties.

However you could simply make that a default export:

export default __globalFluxStorage.state.property.property.property.property;

import DynamicModule from 'dynamic-module';

If you want a named import, you'll have to declare the name in your export:

export var DynamicModule = __globalFluxStorage.state.property.property.property.property;

import {DynamicModule} from 'dynamic-module';

This would also allow changing the value later when it's not available right at the time of the module being loaded:

export var DynamicModule;
…
DynamicModule = __globalFluxStorage.state.property.property.property.property;

(although in that case you might want to consider exporting a Promise or EventEmitter instead)

You can export an object with a getter or export a function if you need to re-evaluate the value every time it is used in imports.

export const _ = {
  get DynamicModuleGetter() {return __globalFluxStorage.state.property.property.property.property}
}

export function DynamicModuleFunction() {return __globalFluxStorage.state.property.property.property.property}

Then in import

import { _, DynamicModuleFunction } from 'dynamic-module'

// getter
const value1 = _.DynamicModuleGetter
const {DynamicModuleGetter} = _        // this evaluates the getter

// function
const value2 = DynamicModuleFunction()

A more elaborate example

let obj = {
  foo: {
    bar: {
      baz: {
        bak: {
          value: 1
        },
        fak: {
          value: 2
        }
      }
    }
  }
}

export const _ = {
  get shortcut() {return obj.foo.bar.baz}
}

export function shortcut() {return obj.foo.bar.baz}

import

import { _, shortcut } from './shortcut'

let g = _.shortcut.bak.value       // g = 1
let f = shortcut().fak.value       // f = 2
let {shortcut: {bak: {value}}} = _ // value = 1

 const temp = { get myGetter() { return 1; } } export const myGetter = temp.myGetter;

Late to the party, but for what it's worth this is what I did in the end (simplified for readability), and it was clean and simple:

const CONFIG = {
  internal: {},
  get props() {
    return this.internal;
  },
  set props(props) {
    this.internal = props;
  }
};

let props = CONFIG.props;

export { props };

import { props } from "path/to/export";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM