Skip to content

Commit bdabc6e

Browse files
authored
Merge pull request #89 from sabin-khatri/sabin
modules example in js
2 parents bc8f764 + 1d60df2 commit bdabc6e

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6+
<title>JS Modules </title>
7+
<script type="module" src="main.js"></script>
8+
<style>
9+
body { font-family: Arial;
10+
padding: 20px;
11+
background: #f4f4f4;
12+
}
13+
.box { max-width: 500px;
14+
margin: auto;
15+
background: white;
16+
padding: 20px;
17+
border-radius: 10px;
18+
}
19+
pre { background: #eee;
20+
padding: 10px;
21+
border-radius: 5px;
22+
}
23+
</style>
24+
</head>
25+
<body>
26+
<div class="box">
27+
<h2>JS Modules – import/export</h2>
28+
<p>Open console to see output!</p>
29+
<pre id="output"></pre>
30+
</div>
31+
</body>
32+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Modules/main.js
2+
3+
import multiply, { add, subtract, PI } from './math.js';
4+
import sayHello, { admin, Person } from './user.js';
5+
6+
// Use math
7+
console.log("5 + 3 =", add(5, 3));
8+
console.log("10 - 4 =", subtract(10, 4));
9+
console.log("5 * 3 =", multiply(5, 3));
10+
console.log("PI =", PI);
11+
12+
// Use user
13+
admin.login();
14+
sayHello();
15+
16+
const sabin = new Person("Sabin Pro");
17+
sabin.greet();
18+
19+
// Show in HTML
20+
document.getElementById('output').textContent = `
21+
add(5,3) = ${add(5,3)}
22+
multiply(5,3) = ${multiply(5,3)}
23+
Person: ${sabin.name}
24+
`.trim();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Modules/math.js
2+
3+
export function add(a, b) {
4+
return a + b;
5+
}
6+
7+
export function subtract(a, b) {
8+
return a - b;
9+
}
10+
11+
export const PI = 3.14;
12+
13+
export default function multiply(a, b) {
14+
return a * b;
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Modules/user.js
2+
3+
4+
export const admin = {
5+
name: "Sabin",
6+
role: "Admin",
7+
login() {
8+
console.log(`${this.name} logged in!`);
9+
}
10+
};
11+
12+
export class Person {
13+
constructor(name) {
14+
this.name = name;
15+
}
16+
greet() {
17+
console.log(`Hi, I'm ${this.name}`);
18+
}
19+
}
20+
21+
// Default export
22+
export default function sayHello() {
23+
console.log("Hello from default export!");
24+
}

0 commit comments

Comments
 (0)