-
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 Closures, how to encapsulate data, Multiple closures an…
…d Loops in closures
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/04_intro_to_closures.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,20 @@ | ||
// Closures | ||
|
||
// Simple Closure | ||
|
||
function outerFunction(){ | ||
const outerVar = "I am Outside!" | ||
|
||
function innerFunction(){ | ||
|
||
console.log(outerVar); | ||
} | ||
|
||
return innerFunction; | ||
} | ||
|
||
const closureFunction = outerFunction() | ||
closureFunction(); // I am outside | ||
|
||
// In this example, innerFunction is a closure. | ||
// It captures variable outerVar from it's lexical scope and retains access to it even after outerFunction has finished executing. |
18 changes: 18 additions & 0 deletions
18
...ai aur Javascript/16 - Lexical Scoping and Closures/05_data_encapsulation_with_closure.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,18 @@ | ||
// Data Encapsulation with Closures | ||
|
||
function createCounter(){ | ||
|
||
let count = 0; | ||
|
||
return function(){ | ||
count += 1 | ||
return count | ||
} | ||
} | ||
|
||
const counter = createCounter() | ||
console.log(counter()) // 1 | ||
console.log(counter()) // 1 | ||
console.log(counter()) // 3 | ||
|
||
// In this example, 'count' is encapsulated within the function returned by createCounter, allowing it to maintain state between calls. |
26 changes: 26 additions & 0 deletions
26
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/06_multiple_closures.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,26 @@ | ||
// Multiple Closures | ||
|
||
function createAdder(x){ | ||
return function(y){ | ||
return x + y | ||
} | ||
} | ||
|
||
const addFive = createAdder(5) | ||
|
||
const addTen = createAdder(10) | ||
|
||
console.log(addFive(2)) // Output: 7 | ||
|
||
console.log(addTen(7)) // Output: 17 | ||
|
||
|
||
function fullName(firstName){ | ||
return function(lastName){ | ||
return `${firstName} ${lastName}` | ||
} | ||
} | ||
|
||
const myName = fullName('Sohail') | ||
|
||
console.log(myName("Shaikh")); // Sohail Shaikh |
26 changes: 26 additions & 0 deletions
26
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/07_closure_in_loops.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,26 @@ | ||
// Closure in Loops | ||
|
||
// Incorrect behavior | ||
var funcs = [] | ||
for(var i = 0; i < 3; i++){ | ||
funcs[i] = function(){ | ||
console.log(i); | ||
} | ||
} | ||
|
||
funcs[0]() // 3 | ||
funcs[1]() // 3 | ||
funcs[2]() // 3 | ||
|
||
|
||
// Corrected with let | ||
|
||
for(let i = 0; i < 3; i++){ | ||
funcs[i] = function(){ | ||
console.log(i); | ||
} | ||
} | ||
|
||
funcs[0]() // 0 | ||
funcs[1]() // 1 | ||
funcs[2]() // 2 |