// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
console.log("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
console.log("2π = " + sum(pi, pi));
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.exp(x);
}
// app.js
import exp, {pi, e} from "lib/mathplusplus";
console.log("e^π = " + exp(pi));
// library.js
// import the book module
import {favoriteBook, Book} from 'book';
// create some books and get their descriptions
let booksILike = [
new Book("Under The Dome", "Steven King"),
new Book("Julius Ceasar", "William Shakespeare")
];
console.log("My favorite book is " + favoriteBook + ".");
console.log("I also like " + booksILike[0].describeBook() + " and " + booksILike[1].describeBook());
// book.js
const favoriteBook = {
title: "The Guards",
author: "Ken Bruen"
}
// a Book class using ES6 class syntax
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
describeBook() {
let description = this.title + " by " + this.author + ".";
return description;
}
}
// exporting looks different from Node.js but is almost as simple
export {favoriteBook, Book};