eggyal
eggyal

Reputation: 125995

How does one "implicitly export an entire module"?

The following is a section from MDN's reference on the JavaScript import statement (with added emphasis):

Import a single export from a module

Given an object or value named myExport which has been exported from the module my-module either implicitly (because the entire module is exported) or explicitly (using the export statement), this inserts myExport into the current scope.

import {myExport} from '/modules/my-module.js';

I know what it means for an object or value to have been exported from a module explicitly (using the export statement), but how can they be exported implicitly (impliedly without using an export statement)? What does it mean for an "entire module" to be exported?

Upvotes: 5

Views: 830

Answers (2)

loganfsmyth
loganfsmyth

Reputation: 161617

I think the wording on this statement is somewhat confusing, assuming I understand it correctly. I think what they mean by "explicitly" would be explicitly named, e.g.

export { foo };
// or others
export var foo;
export function foo(){}
export class foo {}
export { foo } from "./foo.js";

whereas implicitly would be one that is not explicitly named, like

export * from "./foo.js";

where then doing

import { foo } from "./mod.js";

would work as long as mod is re-exporting foo from the foo.js file.

Upvotes: 2

Nikko Khresna
Nikko Khresna

Reputation: 1009

lets make it this way

  1. Given an object or value named myExport which has been exported implicitly (because the entire module is exported) from a module

or

  1. Given an object or value named myExport which has been exported explicitly (using the export statement) from a module

so i can export an object or func like this

// ./modules/my-module.js
export default UserApi = {
  myExport: function() {
   console.log(please make api call)
  }
}


// ./otherfile.js
import {myExport} from '/modules/my-module.js';

i never explicitly export myExport but you can import myExport without importing UserApi which i explicitly export

Upvotes: -1

Related Questions