Modules in ES6
-
Introduction
-
Models
Default export
Used when we only want to export one thing from a module.
export default 'example';
Named export
Used to export multiple things from the same module.
export const add = (a, b) => a + b; // this is a named export, because it has a name for us to import it from the index.js file.
export const multiply = (a, b) => a * b;
export const ID = 23;
Notes
a) File syntext: first letter in uppercase, as in Search.js.
b) Importing a model: no need to put the .js at the end. Importing Search.js as Search.
c) Import a variable and change it's name:
Case 01
import { add as a, multiply as m, ID } from './views/searchView';
console.log(`Using imported functions! ${a(ID, 2)} and ${m(3, 5)}. ${str}`);
Case 02
import * as searchView from './views/searchView'; creates an object searchView and imports into it.
console.log(`Using imported functions! ${searchView.add(searchView.ID, 2)} and ${searchView.multiply(3, 5)}. ${str}`);