Skip to content

Commit f378e33

Browse files
committed
Export and importing defaults
1 parent c66eb5f commit f378e33

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

manuscript/11-Modules.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function sum(num1, num2) {
134134
export { sum as add };
135135
```
136136

137-
Here, the `sum()` function is exported under the `add()`. That means when another module wants to import this function, it will have to use the name `add` instead:
137+
Here, the `sum()` function (`sum` is the *local name*) is exported as `add()` (`add` is the *exported name*). That means when another module wants to import this function, it will have to use the name `add` instead:
138138

139139
```js
140140
import { add } from "example";
@@ -148,4 +148,54 @@ console.log(typeof add); // "undefined"
148148
console.log(sum(1, 2)); // 3
149149
```
150150

151-
This code imports the `add()` function and renames it to `sum()`. That means there is no identifier named `add` in this module.
151+
This code imports the `add()` function (the *import name*) and renames it to `sum()` (the local name). That means there is no identifier named `add` in this module.
152+
153+
## Exporting and Importing Defaults
154+
155+
The module syntax is really optimized for exporting and importing default values from modules. The default value for a module is a single variable, function, or class as specified by the `default` keyword. For example:
156+
157+
```js
158+
export default function(num1, num2) {
159+
return num1 + num2;
160+
}
161+
```
162+
163+
This module exports a function as the default. The `default` keyword indicates that this is a default export and the function doesn't require a name because the module itself represents the function.
164+
165+
W> You can only have one default export per module. It is a syntax error to use the `default` keyword with multiple exports.
166+
167+
You can import a default value from a module using the following syntax:
168+
169+
```js
170+
// import the default
171+
import sum from "example";
172+
173+
console.log(sum(1, 2)); // 3
174+
```
175+
176+
This import statement imports the default from the module `"example"`. Note that there are no curly braces used in this case, as would be with a non-default export. The local name `sum` is used to represent the function that the module exports. This syntax is the cleanest as it's anticipated to be the dominant form of import on the web, allowing you to use already-existing object, such as:
177+
178+
```js
179+
import $ from "jquery";
180+
```
181+
182+
For modules that export both a default and one or more non-defaults, you can import them with one statment. For instance, suppose you have this module:
183+
184+
```js
185+
export let color = "red";
186+
187+
export default function(num1, num2) {
188+
return num1 + num2;
189+
}
190+
```
191+
192+
You can then import both `color` and the default function using the following:
193+
194+
```js
195+
import sum, { color } from "example";
196+
197+
console.log(sum(1, 2)); // 3
198+
console.log(color); // "red"
199+
```
200+
201+
The comma separates the default local name from the non-defaults (which are also surrounded by curly braces).

0 commit comments

Comments
 (0)