-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about Modules, import and export, default and named import/ex…
…port
- Loading branch information
Showing
8 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
03 - Chai aur Javascript/17 - Modules/circular_dependencies_a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
5 changes: 5 additions & 0 deletions
5
03 - Chai aur Javascript/17 - Modules/circular_dependencies_b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function greet(name) { | ||
return `Hello ${name}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { add, subtract } from "./mathOperations.js"; | ||
|
||
export default { | ||
add, | ||
subtract, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { subtract } from "./math.js"; |