Import

Now, let's create a module app.js to import the modules we just created.

Let's have a look at app.js file:

1.  import User from './user.js';    2.  import * as Roles from './roles.js';3.  import completeTask from './tasks.js';4.  import {completedCount} from './tasks.js';5. 6.  let user = new User('Ted', Roles.USER);     7.  completeTask(user);    8.  console.log(`Total completed ${completedCount}`);9.  // completedCount++; 10. // Only to show that you can change imported object.11. // NOT a good practice to do it though.12. User.prototype.walk = function () {13.   console.log(`${this.name} walks`);14. };15. user.walk();

In line 1, we use default import to import the User class from the user.js module. You can use a different name other than User ...

Get Building Applications with Spring 5 and Vue.js 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.