Skip to content

Commit 19b5ec5

Browse files
committed
feat: 🎸 added next topics of namaste js
added next topics of namaste js
1 parent 32c4ec9 commit 19b5ec5

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// dummy() // Executes since dummy is stored while memory creation phase
2+
// dummy2() // TypeError b is no a function
3+
// * Function statement aka Function Declaration
4+
function dummy() {
5+
console.log('dummy called')
6+
}
7+
8+
// * Function expression
9+
var dummy2 = function () {
10+
console.log('dummy 2 called')
11+
return 'dummy2'
12+
}
13+
14+
// * Anonymous Function
15+
/**
16+
function(){
17+
18+
}
19+
*/
20+
21+
// * Named function expression
22+
const dummy3 = function newDummy() {
23+
console.log('newDummy called')
24+
}
25+
26+
dummy3()
27+
// newDummy() // ReferenceError: newDummy is not defined
28+
29+
// Difference Parameters and Arguments
30+
const dummy4 = function ({ parameter1, parameter2 }) {
31+
console.log({ parameter1, parameter2 })
32+
}
33+
34+
dummy4({ parameter1: 'argument 1', parameter2: 'argument 2' }) // the values which are being passed
35+
36+
// * First class functions in JS
37+
// * The ability to use functions as values, and can be passed as arguments to another functions and can be returned from the functions this ability is known as first class functions
38+
39+
function testFunction() {
40+
console.log('Test function logged')
41+
}
42+
43+
const functionRunner = function (param) {
44+
return param
45+
}
46+
47+
const val = functionRunner(testFunction)
48+
val()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// JavaScript is a synchronous and single-threaded language
2+
//* What is a Callback Function in JavaScript
3+
4+
// function x() {}
5+
6+
// here y is callback function
7+
// x(function y() {})
8+
9+
// Blocking the main thread
10+
// * we should not block the main thread, any operation which is supposed to take much time should be async.
11+
setTimeout(() => {
12+
console.log({
13+
'Current Time': new Date().toLocaleString(),
14+
})
15+
console.log('Timer stopped')
16+
}, 5000)
17+
18+
function x(func) {
19+
console.log('x')
20+
func()
21+
}
22+
23+
x(function y() {
24+
console.log('y')
25+
})
26+
27+
console.log({
28+
'Current Time': new Date().toLocaleString(),
29+
})
30+
31+
// Deep about Event listeners
32+
// Closures Demo with Event Listeners
33+
function attachEventListener() {
34+
// * this will form a closure
35+
let count = 0
36+
const btn = document.getElementById('my-btn')
37+
btn.addEventListener('click', function () {
38+
console.log({ msg: 'My button clicked' })
39+
count++
40+
})
41+
console.log({ count })
42+
}
43+
attachEventListener()
44+
45+
// Power of Callbacks?
46+
// Scope Demo with Event listeners

2024 Prep/namaste_js/15_event_loop.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
/**
3+
* * Web APIs
4+
* 1. setTimeout()
5+
* 2. DOM APIs
6+
* 3. fetch()
7+
* 4. localStorage()
8+
* 5. console.log()
9+
* 6. location
10+
*
11+
* * All of the above Web APIs are accessible via window object in JS call stack
12+
*/
13+
14+
// console.log('Start')
15+
// setTimeout(() => {
16+
// console.log('Callback')
17+
// }, 3000)
18+
// console.log('End')
19+
20+
/*
21+
22+
console.log('Start')
23+
24+
* if the event handler will stay in the web apis environment, we explicitly remove that event listener or we close the browser
25+
document.getElementById('btn').addEventListener('click', function cb() {
26+
console.log('Button clicked')
27+
})
28+
console.log('End')
29+
*/
30+
31+
/**
32+
// * Why do we need a callback queue
33+
For handling the callbacks to execute in order and sending it to call stack one by one
34+
*/
35+
36+
// console.log('A')
37+
// setTimeout(() => console.log('C'), 1000)
38+
// Promise.resolve().then(() => console.log('B'))
39+
// console.log('D')
40+
41+
/*
42+
* Execution order:
43+
1. A Synchronous calls, no timers or promises involved so just goes to call stack and gets executed
44+
2. D -\\ -
45+
3. B Since promises runs in Microtask queue it will be executed first (Microtask queue has higher priority in than callback queue)
46+
4. C After the timer completed the callback goes to callback queue and executes it in the end
47+
48+
*/
49+
50+
console.log('A')
51+
setTimeout(() => console.log('C'), 1000)
52+
Promise.resolve().then(setTimeout(() => console.log('B'), 1000))
53+
console.log('D')
54+
55+
/**
56+
* A D C B
57+
* Since in the promises there's also a setTimeout so it will run in the order. First callback in setTimeout and second callback in setTimeout
58+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// console.log('START')
2+
3+
// setTimeout(() => {
4+
// console.log('Callback')
5+
// }, 5000)
6+
7+
// console.log('END')
8+
9+
/*
10+
* some code which need lot of time to execute
11+
* This code will block the main thread, and won't allow the callback queue to perform it's operations
12+
* So if the following line of code is taking 10 seconds to execute,
13+
* the callback inside the setTimeout will be ran after that, since the main thread is still blocked
14+
*/
15+
16+
// -------------------------------
17+
// * Simulation of blocking thread
18+
19+
console.log('START')
20+
setTimeout(() => {
21+
console.log('Callback')
22+
}, 1000)
23+
24+
console.log('END')
25+
26+
let startDate = new Date().getTime()
27+
let endDate = startDate
28+
29+
while (endDate < startDate + 5000) {
30+
endDate = new Date().getTime()
31+
}
32+
33+
console.log('While expires')
34+
35+
/*
36+
Output:
37+
38+
START
39+
END
40+
While expires (Expired after 5 sec)
41+
Callback (After main thread is free, this callback is executed)
42+
*/

0 commit comments

Comments
 (0)