Modules
Course- Javascript >
As JavaScript applications grow in complexity, a means needs to be found to make objects declared in one file available in others. By this means, larger projects can be written in a modular fashion.
By default, anything you declare in one file is not available outside of that file. In ECMAScript 6, though, you can use the export keyword to make it available.
Here’s an example of how to export a class:
// this code appears in file1.js export default function Car(Color, Year, Make, Miles) { this.color = Color; this.year = Year; this.make = Make; this.odometerReading = Miles; this.setOdometer = function(newMiles) { this.odometerReading = newMiles; } // this object can be imported by other files
And in the receiving file:
// this code appears in file2.js import Car from 'file1'; var ferrari = new Car('red', 1986, 'Dino', 75500);