fixes
This commit is contained in:
parent
f5f31e8e08
commit
899a3cea20
2 changed files with 18 additions and 14 deletions
|
@ -103,7 +103,7 @@ Well, there are few reasons.
|
|||
export function becomeSilent() { ... }
|
||||
```
|
||||
|
||||
Now if we in fact need only one of them in our project:
|
||||
Now if we only use one of `lib.js` functions in our project:
|
||||
```js
|
||||
// 📁 main.js
|
||||
import {sayHi} from './lib.js';
|
||||
|
@ -218,8 +218,7 @@ export default function(user) { // no function name
|
|||
export default ['Jan', 'Feb', 'Mar','Apr', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
```
|
||||
|
||||
That's fine, because `export default` is only one per file, so `import` always knows what to import.
|
||||
Contrary to that, omitting a name for named imports would be an error:
|
||||
That's fine, because `export default` is only one per file. Contrary to that, omitting a name for named imports would be an error:
|
||||
|
||||
```js
|
||||
export class { // Error! (non-default export needs a name)
|
||||
|
@ -229,9 +228,9 @@ export class { // Error! (non-default export needs a name)
|
|||
|
||||
### "Default" alias
|
||||
|
||||
The "default" word is a kind of "alias" for the default export, for scenarios when we need to reference it somehow.
|
||||
The "default" keyword is used as an "alias" for the default export, for standalone exports and other scenarios when we need to reference it.
|
||||
|
||||
For example, if we already have a function declared, that's how to `export default` it:
|
||||
For example, if we already have a function declared, that's how to `export default` it (separately from the definition):
|
||||
|
||||
```js
|
||||
function sayHi(user) {
|
||||
|
@ -278,7 +277,7 @@ new User('John');
|
|||
|
||||
### Should I use default exports?
|
||||
|
||||
One should be careful about using default exports, because they are somewhat more different to maintain.
|
||||
One should be careful about using default exports, because they are more difficult to maintain.
|
||||
|
||||
Named exports are explicit. They exactly name what they import, so we have that information from them, that's a good thing.
|
||||
|
||||
|
@ -286,12 +285,15 @@ Also, named exports enforce us to use exactly the right name to import:
|
|||
|
||||
```js
|
||||
import {User} from './user.js';
|
||||
// import {MyUser} won't work, the name must be {User}
|
||||
```
|
||||
|
||||
For default exports, we need to create a name on our own:
|
||||
For default exports, we always choose the name when importing:
|
||||
|
||||
```js
|
||||
import MyUser from './user.js'; // could be import Anything..., and it'll work
|
||||
import User from './user.js'; // works
|
||||
import MyUser from './user.js'; // works too
|
||||
// could be import Anything..., and it'll be work
|
||||
```
|
||||
|
||||
So, there's a little bit more freedom that can be abused, so that team members may use different names for the same thing.
|
||||
|
@ -409,16 +411,18 @@ Import:
|
|||
- `import {default as x} from "mod"`
|
||||
- Everything:
|
||||
- `import * as obj from "mod"`
|
||||
- Only fetch/evaluate the module, don't import:
|
||||
- Import the module (it runs), but do not assign it to a variable:
|
||||
- `import "mod"`
|
||||
|
||||
We can put import/export statements below or after other code, that doesn't matter.
|
||||
We can put import/export statements at the top or at the bottom of a script, that doesn't matter.
|
||||
|
||||
So this is technically fine:
|
||||
```js
|
||||
sayHi();
|
||||
|
||||
import {sayHi} from './say.js'; // import at the end of the file
|
||||
// ...
|
||||
|
||||
import {sayHi} from './say.js'; // import at the end of the script
|
||||
```
|
||||
|
||||
In practice imports are usually at the start of the file, but that's only for better convenience.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue