You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manuscript/11-Modules.md
+52-2Lines changed: 52 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -134,7 +134,7 @@ function sum(num1, num2) {
134
134
export { sumasadd };
135
135
```
136
136
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:
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
+
exportdefaultfunction(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
+
importsumfrom"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
+
exportlet color ="red";
186
+
187
+
exportdefaultfunction(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
+
importsum, { 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