ES6 Modules Flashcards
What is value of ‘this’ at top level of a module?
undefined (as opposed to window, which it would be if not in a module)
Name 3 ways modules semantics are different than script semantics.
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 many times will example.js be executed in the following:
import { sum } from ‘./example.js’;
import { subtract } from ‘./example.js’;
Once. After the code to import the module executes in first import, it’s kept in memory and reused.
Can modules be imported conditionally, as in:
if (flag) { import ‘.somefile.js’ }
No. Both import and export must be used outside other statements or functions.
Export function sum(x,y){…} using a name other than sum, e.g. ‘add’
export { sum as add }
What are two ways to export a module using the ‘default’ keyword?
- export default myFunc( arg ){…}
2. or name the variable, function, or class first, then export it, export default myFunc;
How do you load a module in a web page?
script type=”module” src=”module1.js” (then close script tag)
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” /
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.