Gurg Hackpof
Gurg Hackpof

Reputation: 1344

ES6 JS: syntax for const export when there is some recursion

I have the following code that works:

import sequelize from 'sequelize';
let A = sequelize.define('a', {
  }, {
    classMethods: {
      someMethod(foo) {
        return A.upsert(foo);
      }
    }
  });

export default A;

However, I would like to put the export default on the same line that the definition of A for clarity. However, since some part of A basically calls itself I'm not able to find out what the right syntax could be.

Upvotes: 0

Views: 419

Answers (2)

Bergi
Bergi

Reputation: 665546

You can use

export { A as default }
const A = …; // whatever

or

let A; // I would avoid this
export default A = …;

There is no shortcut, export default works only with function or class declarations.

Upvotes: 2

Mike Cluck
Mike Cluck

Reputation: 32531

The short answer is: you can't.

The long answer is: when doing export default something you are specifying that a given value will be exported from the module and is intrinsically nameless (hence, the default part).

Unlike with CommonJS, you don't have a handle for whatever you're exporting. What you're wanting to do is equivalent to calling an anonymous function recursively. How could you do that without a reference?

Upvotes: 1

Related Questions