Skip to content

Commit

Permalink
Learned about Modules, import and export, default and named import/ex…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
sohail019 committed Jul 31, 2024
1 parent 2667cac commit aaba485
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bFunction } from "./circular_dependencies_b";
export function aFunction() {
console.log("aFunction");
bFunction();
}

aFunction();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { aFunction } from "./circular_dependencies_a";
export function bFunction() {
console.log("bFunction");
aFunction();
}
7 changes: 7 additions & 0 deletions 03 - Chai aur Javascript/17 - Modules/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let count = 0;

console.log("Module loaded");
export function increment() {
count++;
return count;
}
3 changes: 3 additions & 0 deletions 03 - Chai aur Javascript/17 - Modules/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function greet(name) {
return `Hello ${name}`;
}
19 changes: 19 additions & 0 deletions 03 - Chai aur Javascript/17 - Modules/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Export variable
export const pi = 3.14159;

// Export Function
export function add(a, b) {
return a + b;
}

// Export Class
export class Calculator {
multiply(x, y) {
return x * y;
}
}

// Re-exporting
export function subtract(a, b) {
return a - b;
}
6 changes: 6 additions & 0 deletions 03 - Chai aur Javascript/17 - Modules/mathUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { add, subtract } from "./mathOperations.js";

export default {
add,
subtract,
};
39 changes: 39 additions & 0 deletions 03 - Chai aur Javascript/17 - Modules/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Importing modules

import { pi, add, Calculator } from "./math.js";

console.log("Pi: ", pi); // Pi: 3.14159

console.log("Addition is ", add(2, 4)); // Addition is 6

const cal = new Calculator();
console.log(`Multiply: ${cal.multiply(12, 5)}`); // Multiply: 60

// Importing Defaults
import greet from "./greet.js";

console.log(greet("Sohail")); // Hello Sohail
console.log(greet("Salman")); // Hello Salman

// Re Exporting
import { subtract } from "./utilities.js";

console.log(subtract(10, 2)); // 8

// Dynamic Imports
async function loadModule() {
const module = await import("./math.js");

console.log(`From Dynamic Import: ${module.add(10, 20)}`); // Output: 30
}

loadModule();

// Module caching
import { increment } from "./counter.js";
console.log(increment()); // Logs: Module Loaded
console.log(increment()); // No additional log

// Facade Pattern
import MathUtils from "./mathUtil.js";
console.log(MathUtils.add(2, 3)); // 5
1 change: 1 addition & 0 deletions 03 - Chai aur Javascript/17 - Modules/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { subtract } from "./math.js";

0 comments on commit aaba485

Please sign in to comment.