ES6 Modules Flashcards

1
Q

What is value of ‘this’ at top level of a module?

A

undefined (as opposed to window, which it would be if not in a module)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name 3 ways modules semantics are different than script semantics.

A

module code automatically runs in strict mode; value of ‘this’ is undefined; variables at top level don’t automatically modify the global scope; modules must export anything that should be available to code outside module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many times will example.js be executed in the following:
import { sum } from ‘./example.js’;
import { subtract } from ‘./example.js’;

A

Once. After the code to import the module executes in first import, it’s kept in memory and reused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can modules be imported conditionally, as in:

if (flag) { import ‘.somefile.js’ }

A

No. Both import and export must be used outside other statements or functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Export function sum(x,y){…} using a name other than sum, e.g. ‘add’

A

export { sum as add }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are two ways to export a module using the ‘default’ keyword?

A
  1. export default myFunc( arg ){…}

2. or name the variable, function, or class first, then export it, export default myFunc;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you load a module in a web page?

A

script type=”module” src=”module1.js” (then close script tag)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what order will following modules execute?
script type=”module” src=”mod1.js” /
script type=”module” src=”mod2.js” /
script type=”module” async src=”mod3.js” /

A

mod1 will execute before mod2, but mod3 is async and will execute as soon as it’s parsed, so we can’t know whether it will go before or after the others.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly