Wrapping Up

JavaScript now provides an effective way to split functions and classes into files or modules. Modules are essential to manage the complexity of large applications and to keep code cohesive and make it easier to reuse. Code within modules run in strict mode. You can be very deliberate about what’s internal to a module and what should be exported. JavaScript offers a variety of options to import classes, functions, variables, and primitives from modules.

In the next chapter we’ll look at promises and the new asynchronous programming features of JavaScript.

Exercises

The imports and exports have some moving parts that can get a bit challenging. Use these exercises to hone in on your understanding. You can find answers to these exerciseshere.

Exercise 1

State true or false for each of the following:

  1. In the execution flow, the code within a module is loaded as many times as it is imported.

  2. An inlined export—that is, placing export when defining a variable—turns the variable into the default export for that module.

  3. If two imported modules have default exports, we will get an error in the importing module.

  4. We should use inline exports where possible instead of explicit exports.

  5. A module can have either a single default export or any number of nondefault exports, but not both.

Exercise 2

When would you use an explicit export instead of an implicit export?

Exercise 3

When a module imports a default export, what name does it see?

Exercise 4

What does the following syntax mean?

 export * from 'some-module';

Exercise 5

A module named fasttrack has many exports, including one default. How can we import all the items exported by that module, including the default one?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.173.40