Skip to content

Commit a81b579

Browse files
committed
chapter 01: ES2015 modules
1 parent b2f6e3a commit a81b579

12 files changed

+376
-1
lines changed

examples/chapter01/17-Book.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default class Book {
2+
constructor(title) {
3+
this.title = title;
4+
}
5+
printTitle() {
6+
console.log(this.title);
7+
}
8+
}

examples/chapter01/17-CalcArea.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const circleArea = r => 3.14 * (r ** 2);
2+
3+
export const squareArea = s => s * s;
4+
5+
// export { circleArea, squareArea }; // {1}
6+
export { circleArea as circle, squareArea as square };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const area = require('./lib/17-CalcArea');
2+
const Book = require('./lib/17-Book');
3+
4+
console.log(area.circle(2));
5+
console.log(area.square(2));
6+
7+
const myBook = new Book('some title');
8+
myBook.printTitle();

examples/chapter01/17-ES2015-ES6-Modules.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="17-ES2015-ES6-Modules.js"></script>
8+
<script type="module" src="17-ES2015-ES6-Modules.js"></script>
9+
<script nomodule src="./lib/17-ES2015-ES6-Modules-bundle.js"></script>
910
</body>
1011
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
// import { circleArea, squareArea } from './17-CalcArea'; // {2}
3+
// import { circleArea as circle } from './17-CalcArea';
4+
5+
// console.log(circleArea(2));
6+
// console.log(squareArea(2));
7+
8+
/* Different way of importing the module */
9+
// import * as area from './17-CalcArea';
10+
// import Book from './17-Book';
11+
12+
import * as area from './17-CalcArea.js'; // we need the .js to run this code in the browser
13+
import Book from './17-Book.js';
14+
15+
console.log(area.circle(2));
16+
console.log(area.square(2));
17+
18+
const myBook = new Book('some title');
19+
myBook.printTitle();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default class Book {
2+
constructor(title) {
3+
this.title = title;
4+
}
5+
printTitle() {
6+
console.log(this.title);
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const circleArea = r => 3.14 * (r ** 2);
2+
3+
export const squareArea = s => s * s;
4+
5+
// export { circleArea, squareArea }; // {1}
6+
export { circleArea as circle, squareArea as square };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as area from './17-CalcArea.mjs';
2+
import Book from './17-Book.mjs';
3+
4+
console.log(area.circle(2));
5+
console.log(area.square(2));
6+
7+
const myBook = new Book('some title');
8+
myBook.printTitle();

examples/chapter01/lib/17-Book.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(function (global, factory) {
2+
if (typeof define === "function" && define.amd) {
3+
define(["module", "exports"], factory);
4+
} else if (typeof exports !== "undefined") {
5+
factory(module, exports);
6+
} else {
7+
var mod = {
8+
exports: {}
9+
};
10+
factory(mod, mod.exports);
11+
global.Book = mod.exports;
12+
}
13+
})(this, function (module, exports) {
14+
"use strict";
15+
16+
Object.defineProperty(exports, "__esModule", {
17+
value: true
18+
});
19+
20+
function _classCallCheck(instance, Constructor) {
21+
if (!(instance instanceof Constructor)) {
22+
throw new TypeError("Cannot call a class as a function");
23+
}
24+
}
25+
26+
var _createClass = function () {
27+
function defineProperties(target, props) {
28+
for (var i = 0; i < props.length; i++) {
29+
var descriptor = props[i];
30+
descriptor.enumerable = descriptor.enumerable || false;
31+
descriptor.configurable = true;
32+
if ("value" in descriptor) descriptor.writable = true;
33+
Object.defineProperty(target, descriptor.key, descriptor);
34+
}
35+
}
36+
37+
return function (Constructor, protoProps, staticProps) {
38+
if (protoProps) defineProperties(Constructor.prototype, protoProps);
39+
if (staticProps) defineProperties(Constructor, staticProps);
40+
return Constructor;
41+
};
42+
}();
43+
44+
var Book = function () {
45+
function Book(title) {
46+
_classCallCheck(this, Book);
47+
48+
this.title = title;
49+
}
50+
51+
_createClass(Book, [{
52+
key: "printTitle",
53+
value: function printTitle() {
54+
console.log(this.title);
55+
}
56+
}]);
57+
58+
return Book;
59+
}();
60+
61+
exports.default = Book;
62+
module.exports = exports["default"];
63+
});

examples/chapter01/lib/17-CalcArea.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(function (global, factory) {
2+
if (typeof define === "function" && define.amd) {
3+
define(["exports"], factory);
4+
} else if (typeof exports !== "undefined") {
5+
factory(exports);
6+
} else {
7+
var mod = {
8+
exports: {}
9+
};
10+
factory(mod.exports);
11+
global.CalcArea = mod.exports;
12+
}
13+
})(this, function (exports) {
14+
"use strict";
15+
16+
Object.defineProperty(exports, "__esModule", {
17+
value: true
18+
});
19+
var circleArea = exports.circleArea = function circleArea(r) {
20+
return 3.14 * Math.pow(r, 2);
21+
};
22+
23+
var squareArea = exports.squareArea = function squareArea(s) {
24+
return s * s;
25+
};
26+
27+
// export { circleArea, squareArea }; // {1}
28+
exports.circle = circleArea;
29+
exports.square = squareArea;
30+
});

0 commit comments

Comments
 (0)