Skip to content

Commit 83e661d

Browse files
committedAug 25, 2024
add javascript execution context
1 parent 6a455f3 commit 83e661d

25 files changed

+355
-123
lines changed
 

‎13_function/19_first_class_function.js ‎13_function/first_class_function.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// First Class Function
22

3-
// Q. What is First Class Function?
3+
// What
44
// - In JavaScript, functions are considered "first-class" citizens, It means they can be treated just like any other value.
55

66
// Functions can be assigned to variables

‎25_asynchronous_javascript/03_call_back/call_back_function.js

-42
This file was deleted.
File renamed without changes.

‎pending_concept/fetch_api/fetch_api.js ‎25_asynchronous_javascript/6_fetch/fetch_api.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ fetch(url)
44
.then((result) => result.json())
55
.then((json) => console.log(json))
66
.catch((error) => console.log(error));
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Call Back Function
2+
3+
// What
4+
// - A call back function is a function which passed as an argument to another function.
5+
// - Call back function called after the main function completes its execution.
6+
7+
// Why
8+
// - To handle asynchronous operations.
9+
// - To control the flow of execution.
10+
11+
// Syntax
12+
function mainFunction(callback) {
13+
// ... do something
14+
callback();
15+
}
16+
17+
function callbackFunction() {
18+
// ... do something after mainFunction finishes
19+
}
20+
21+
mainFunction(callbackFunction);
22+
23+
// How
24+
// - Sometimes we need to execute a functions in certain order.
25+
26+
// Example 1: Calling functions in a order manually.
27+
function orderPizza() {
28+
return console.log("Pizza order has been placed.");
29+
}
30+
function deliverPizza() {
31+
return console.log("Pizza has been delivered.");
32+
}
33+
34+
orderPizza();
35+
deliverPizza();
36+
37+
// Example 2: Nesting function inside the function to execute sequence wise.
38+
function calculation(number1, number2) {
39+
const result = number1 + number2;
40+
displayResult(result);
41+
}
42+
43+
function displayResult(result) {
44+
console.log(result);
45+
}
46+
47+
calculation(5, 10);
48+
49+
// - The problem with the first example above, is that you have to call two functions to display the result.
50+
// - The problem with the second example, is that you cannot prevent the calculator function from displaying the result.
51+
52+
// Example 3: Now it is time to bring in a callback.
53+
function calculation2(number1, number2, callback) {
54+
const result = number1 + number2;
55+
callback(result);
56+
}
57+
58+
function display2(result) {
59+
console.log(result);
60+
}
61+
62+
calculation2(5, 10, display2);
63+
64+
// Note
65+
// - JavaScript functions are executed in the sequence they are called. Not in the sequence they are defined.
66+
// - Overusing callbacks can lead to "callback hell", making code hard to read and maintain. Consider using Promises or async/await for complex asynchronous operations.
67+
68+
// Analogy
69+
// - Let's imagine you're going to a restaurant and you want to place an order. You have a special request: you want to be notified when your order is ready so that you can pick it up.
70+
// - In this scenario, you are the main program or the "caller", and the restaurant is the function you're calling. The restaurant has a chef who will prepare your order, and you need a way to be notified when the order is ready.
71+
// - Here's where the concept of a callback comes in. You give the restaurant your phone number and ask them to call you back when the order is ready. In this case, your phone number is the callback function.
72+
// - In JavaScript, a callback function is similar to giving your phone number to the restaurant. It's a function that you provide as an argument to another function. The main function (the "caller") will call the callback function at an appropriate time, usually when a certain task or operation is completed.

‎25_asynchronous_javascript/03_call_back/call_back_hell.js ‎25_asynchronous_javascript/call_back/2_call_back_hell.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22

33
// https://youtu.be/bx9xYPt2tdc
44

5-
// Function 1
65
function firstFunction(callback) {
76
setTimeout(function () {
87
console.log("First function executed.");
98
callback();
109
}, 1000);
1110
}
1211

13-
// Function 2
1412
function secondFunction(callback) {
1513
setTimeout(function () {
1614
console.log("Second function executed.");
1715
callback();
18-
}, 1000);
16+
}, 3000);
1917
}
2018

21-
// Function 3
2219
function thirdFunction(callback) {
2320
setTimeout(function () {
2421
console.log("Third function executed.");
2522
callback();
26-
}, 1000);
23+
}, 2000);
2724
}
2825

2926
// Callback Hell

‎7_heap_and_stack/01_heap_and_stack.md

-50
This file was deleted.

‎7_heap_and_stack/heap_memory.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Heap Memory
2+
3+
// What
4+
// - A region in a computer's memory where objects and functions are stored in JavaScript.
5+
// - It's used for dynamic memory allocation, meaning the size and location of memory blocks are determined at runtime.
6+
// - Unlike the stack, which has a fixed size, the heap can grow and shrink as needed.
7+
8+
// Why
9+
// - Storing objects and functions: Objects, arrays, and functions are typically stored in the heap due to their dynamic nature and variable sizes.
10+
// - Flexibility: The heap allows for dynamic memory allocation, enabling the creation of objects and variables at runtime.
11+
// - Automatic memory management: JavaScript's garbage collector automatically frees memory allocated in the heap when objects are no longer reachable.
12+
13+
// How
14+
// - Allocation: When an object, array, or function is created, a block of memory is allocated in the heap.
15+
// - Reference: A reference to the allocated memory block is stored on the stack, allowing the object to be accessed.
16+
// - Garbage Collection: The JavaScript engine periodically scans the heap to identify objects that are no longer reachable (not referenced by any other objects). These objects are marked for deletion, and their memory is reclaimed.
17+
18+
// Example
19+
const object1 = {
20+
key1: "value1",
21+
};
22+
23+
const object2 = object1;
24+
object2.key1 = "value2";
25+
26+
console.log(object1.key1);
27+
28+
// Note
29+
// - In heap memory you will be refer to the original value.
30+
// - If you make change. It will reflect to the original value.

‎7_heap_and_stack/stack_memory.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Stack Memory
2+
3+
// Video: https://youtu.be/7gwc-1czolw?si=QrGmJvmijiOURUkf
4+
// Article: https://medium.com/@iarsham/stack-and-heap-in-js-c09aeabf3967
5+
6+
// What
7+
// - The stack is a data structure used in JavaScript to manage function calls and local variables. It operates on a Last-In, First-Out (LIFO) principle, meaning the most recently added item is the first to be removed.
8+
9+
// Why
10+
// - Local Variables: Keep track of the local variables via stack memory.
11+
// - Call Stack: When you call a function. That call will be added to the stack memory in stack order until the function execution completes.
12+
13+
// How
14+
// - The stack is used for static memory allocation.
15+
// - Primitive datatype stored in the stack memory.
16+
// - Stack provide the copy of the value
17+
18+
// Example
19+
let variable1 = "value1";
20+
let variable2 = variable1;
21+
variable2 = "value2";
22+
23+
console.log(variable1);
24+
console.log(variable2);
25+
26+
// Note
27+
// - In Stack Memory it will give you the copy of the original value.

‎README.md

+18-25
Original file line numberDiff line numberDiff line change
@@ -155,39 +155,32 @@
155155
11. Abstraction
156156
12. Polymorphism
157157

158-
2. Asynchronous JavaScript
158+
1. JavaScript Under the Hood
159+
160+
1. Compiler
161+
2. Interpreter
162+
3. Compile Time
163+
4. Runtime
164+
165+
1. JavaScript Execution Context
166+
167+
1. Context
168+
2. Execution Context
169+
1. Global Execution Context
170+
1. Memory Creation Phase
171+
2. Execution Phase
172+
2. Function Execution Context
173+
174+
1. Asynchronous JavaScript
159175
1. Asynchronous Operations
160176
2. setTimeout & setInterval
161177
3. Call back
162178
1. Call back function
163179
2. Call back hell
164180
4. Promise
181+
5. Fetch
165182

166183
<!-- OOP -->
167184

168185
1. Literal Object
169186
2. Constructor Function
170-
171-
# Monospace Font
172-
173-
1. Fira Mono Light
174-
2. JetBrains Mono ExtraLight
175-
3. Commit Mono
176-
4. Fira Mono
177-
5. Monaspace
178-
6. MonoLisa
179-
7. Source Code Pro
180-
8. Cascadia Code
181-
9. JetBrains Mono
182-
10. Maple Mono
183-
184-
# Theme - Pitch Dark Background
185-
186-
1. Bluloco
187-
2. Dracula
188-
3. Github dark
189-
4. One Dark Mono
190-
5. Ayu - Dark
191-
6. Noctis - Obscuro
192-
7. Monokai Night
193-
8. Night Owl

‎code_test_space.js

+9
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@
2828

2929
// // promise1;
3030

31+
function myFirst() {
32+
console.log("Hello");
33+
}
3134

35+
function mySecond() {
36+
console.log("Goodbye");
37+
}
38+
39+
mySecond();
40+
myFirst();
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Context
2+
3+
https://youtu.be/ByhtOgF6uYM?si=CSWhLVOTp682uLUD
4+
5+
### What
6+
7+
Context in programming refers to the environment or circumstances in which a piece of code operates. It encompasses the surrounding information, data, and conditions that influence the code's behavior.
8+
9+
### Why
10+
11+
Understanding context is crucial for:
12+
13+
- Debugging issues
14+
- Code optimization
15+
- Security
16+
- Code maintainability
17+
18+
### How
19+
20+
Context is established through:
21+
22+
- Global variables
23+
- Function arguments
24+
- Object properties
25+
- Database connections
26+
- User interface elements
27+
- Execution environment
28+
29+
### Example
30+
31+
Imagine a function that calculates a user's total income. The context for this function would include:
32+
33+
- The user's income data
34+
- Tax rates
35+
- Deduction rules
36+
37+
### Important Note
38+
39+
Context can vary depending on the programming language, framework, and application. It's essential to consider the context when writing and understanding code.
40+
41+
### Analogy
42+
43+
Context is like the stage on which a play unfolds. The actors (code) perform their roles based on the setting, props, and other characters (data and environment).

0 commit comments

Comments
 (0)
Please sign in to comment.