Skip to content

Commit 915ddcd

Browse files
feat: add promise
1 parent 31bb02e commit 915ddcd

20 files changed

+428
-12
lines changed

13_function/1_function.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// - When argument doesn't pass it will take value as a undefined (In other word empty function return undefine).
1212
// - functionName -> Reference (function definition)
1313
// - functionName() -> Execution (Calling)
14+
// - If you use {}, you need to use return keyword for getting value form the function.
1415

1516
// Example
1617
function myFunction(parameter1, parameter2) {
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
// Normal Function
1+
// Function Declaration
22
function myFunction(parameter) {
33
console.log(parameter);
44
return;
55
}
66

77
// Function Expression
8-
const myVariable = function myFunction(parameter) {
8+
// Named Function Expression
9+
const myVariable1 = function myFunction(parameter) {
910
console.log(parameter);
1011
return;
1112
};
1213

13-
// Anonymous Function
14-
const myVariable1 = function myFunction(parameter) {
14+
// Anonymous Function Expression
15+
const myVariable = function (parameter) {
1516
console.log(parameter);
1617
return;
1718
};
1819

1920
// Function inside the object
2021
// - Do not confuse between storing function inside variable and storing function inside object using key.
21-
// - When you store function inside variable it's function expression and also be anonymous function.
22+
// - When you store function inside variable it's function expression and also it is anonymous function.
2223
// - But when you store function inside object using the key and value pair. It's only anonymous function.
2324
const myObject = {
24-
myFunction: function myFunction(parameter) {
25+
myFunction: function (parameter) {
2526
console.log(parameter);
2627
return;
2728
},
28-
};
29+
};
30+
31+
// Arrow Function (Anonymous by Default)
32+
const arrowFunction = (parameter) => {
33+
console.log(parameter);
34+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
| Feature | Function Declaration | Named Function Expression | Anonymous Function Expression | Arrow Function |
2+
| -------------------------------- | ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | -------------------------------------------------------------- |
3+
| **Syntax** | `function functionName(parameter) { ... }` | `const variable = function functionName(parameter) { ... }` | `const variable = function(parameter) { ... }` | `const variable = (parameter) => { ... }` |
4+
| **Name** | Yes | Yes | No | No |
5+
| **Named Recursion** | Yes | Yes | No | No |
6+
| **Hoisting** | Yes | No | No | No |
7+
| **Invocation before Definition** | Allowed | Not allowed | Not allowed | Not allowed |
8+
| **Reference** | `functionName()` is the function reference | `functionName()` is the function reference but inside the function within scope. For outside `variable()` is the function reference | `variable()` is the variable reference | `variable()` is the variable reference |
9+
| **Reassignment** | Cannot be reassigned | Can be reassigned | Can be reassigned | Can be reassigned |
10+
| **Scope** | Function name available throughout the scope | Function name available throughout the scope | Only accessible through the variable it's assigned to | Only accessible through the variable it's assigned to |
11+
| **Arguments Object** | Has its own `arguments` object | Has its own `arguments` object | Has its own `arguments` object | No `arguments` object, uses `arguments` of surrounding context |
12+
| **`this` Binding** | Dynamic (`this` depends on how the function is called) | Dynamic (`this` depends on how the function is called) | Dynamic (`this` depends on how the function is called) | Lexical (`this` is bound to the surrounding scope) |
13+
| **Constructor** | Can be used as a constructor (with `new` keyword) | Can be used as a constructor (with `new` keyword) | Can be used as a constructor (with `new` keyword) | Cannot be used as a constructor |
14+
| **Debugging/Stack Traces** | Function name appears in stack traces | Function name appears in stack traces | Variable name or "anonymous" in stack traces | Variable name or "anonymous" in stack traces |

15_this_keyword/1_this_keyword.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ const outerObject = {
8080
},
8181
};
8282
console.log(outerObject.innerObject.arrowFunction());
83-
// `this` just doesn't care. It won't show the key1's -> va lue.
83+
// `this` just doesn't care. It won't show the key1's -> value.

24_event/event_basic_1.html

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body style="background-color: #414141; color: #fff">
9+
<h2>Amazing Images</h2>
10+
<div>
11+
<ul id="images">
12+
<li>
13+
<img
14+
width="200px"
15+
id="photoshop"
16+
src="https://images.pexels.com/photos/3561339/pexels-photo-3561339.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
17+
alt="photoshop"
18+
/>
19+
</li>
20+
<li>
21+
<img
22+
width="200px"
23+
id="japan"
24+
src="https://images.pexels.com/photos/3532553/pexels-photo-3532553.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
25+
alt=""
26+
/>
27+
</li>
28+
<li>
29+
<img
30+
width="200px"
31+
id="river"
32+
src="https://images.pexels.com/photos/3532551/pexels-photo-3532551.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
33+
alt=""
34+
/>
35+
</li>
36+
<li>
37+
<img
38+
width="200px"
39+
id="owl"
40+
src="https://images.pexels.com/photos/3532552/pexels-photo-3532552.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
41+
alt=""
42+
/>
43+
</li>
44+
<li>
45+
<img
46+
width="200px"
47+
id="prayer"
48+
src="https://images.pexels.com/photos/2522671/pexels-photo-2522671.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
49+
alt=""
50+
/>
51+
</li>
52+
<li>
53+
<a style="color: aliceblue" href="https://google.com" id="google"
54+
>Google</a
55+
>
56+
</li>
57+
</ul>
58+
</div>
59+
</body>
60+
<script>
61+
// (Not Recommended)
62+
// document.getElementById("owl").onclick = function () {
63+
// alert(`owl clicked`);
64+
// };
65+
66+
// Old ways to working with event and eventObject
67+
// attachEvent(); -> It was use in internet explore.
68+
// jQuery -> It was famous before ReactJS.
69+
70+
// Important Events:
71+
// type, timeStamp, defaultPrevent
72+
// target, toElement, sourceElement, currentTarget
73+
// clientX, clientY, screenX, screenY
74+
// altkey, ctrlkey, shiftkey, keyCode
75+
76+
// Event Propagation:
77+
// 1. Bubbling phase
78+
// - It evaluate events bottom to top.
79+
// - By default value is false.
80+
// 2. Capture phase
81+
// - It evaluate events top to bottom.
82+
// - We manually change need to change value to true.
83+
84+
// document.getElementById("images").addEventListener(
85+
// "click",
86+
// function (e) {
87+
// alert(`image list has been clicked`);
88+
// },
89+
// false
90+
// );
91+
// document.getElementById("owl").addEventListener(
92+
// "click",
93+
// function (e) {
94+
// alert(`owl clicked`);
95+
// e.stopPropagation(); // Stop event bubbling
96+
// },
97+
// false
98+
// );
99+
100+
// document.getElementById("google").addEventListener(
101+
// "click",
102+
// function (e) {
103+
// e.preventDefault();
104+
// // e.stopPropagation();
105+
// console.log("google clicked");
106+
// },
107+
// false
108+
// );
109+
110+
document.querySelector("#images").addEventListener(
111+
"click",
112+
function (e) {
113+
// console.log(e.target);
114+
// console.log(e.target.parentNode);
115+
console.log(e.target.tagName);
116+
if (e.target.tagName === "IMG") {
117+
console.log(e.target.id);
118+
let removeImage = e.target.parentNode;
119+
removeImage.remove();
120+
}
121+
// removeImage.parentNode.removeChild(removeImage);
122+
},
123+
false
124+
);
125+
</script>
126+
</html>

24_event/event_basic_2.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body style="background-color: #414141; color: #fff">
9+
<h1></h1>
10+
<button id="stopButton">Stop</button>
11+
</body>
12+
<script>
13+
const sayName = function () {
14+
console.log("Brijesh");
15+
};
16+
17+
const changeTitle = function () {
18+
document.querySelector("h1").innerHTML = "Event Basic - 2";
19+
};
20+
21+
const changeMe = setTimeout(changeTitle, 2000);
22+
23+
document
24+
.querySelector("#stopButton")
25+
.addEventListener("click", function () {
26+
clearTimeout(changeMe);
27+
console.log("Stopped");
28+
});
29+
</script>
30+
</html>

24_event/event_basic_3.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1>Event Basic - 3</h1>
10+
<button id="startButton">Start</button>
11+
<button id="stopButton">Stop</button>
12+
</body>
13+
<script>
14+
// const sayDate = function (thirdParameter) {
15+
// // console.log(thirdParameter, Date.now());
16+
// };
17+
// const intervalID = setInterval(sayDate, 1000, "thirdArgument");
18+
19+
const startPrintingDate = function () {
20+
console.log(Date.now());
21+
};
22+
23+
const intervalID = setInterval(startPrintingDate, 1000);
24+
25+
document
26+
.querySelector("#startButton")
27+
.addEventListener("click", startPrintingDate);
28+
29+
document
30+
.querySelector("#stopButton")
31+
.addEventListener("click", function () {
32+
clearInterval(intervalID);
33+
});
34+
</script>
35+
</html>
Loading

26_api_request/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body style="background-color: #414141; color: #fff">
9+
<div id="userAvatar"></div>
10+
<div id="followers"></div>
11+
</body>
12+
<script>
13+
const requestURL = "https://api.github.com/users/iambrijeshtoo";
14+
const xmlRequest = new XMLHttpRequest();
15+
let data;
16+
17+
xmlRequest.open("GET", requestURL);
18+
19+
xmlRequest.onreadystatechange = function () {
20+
// console.log(xmlRequest.readyState);
21+
if (xmlRequest.readyState === 4) {
22+
data = JSON.parse(this.responseText);
23+
// console.log(data);
24+
// console.log(data.followers);
25+
26+
document.querySelector(
27+
"#userAvatar"
28+
).innerHTML = `<img src="${data.avatar_url}" alt="User Avatar"/>`;
29+
document.querySelector(
30+
"#followers"
31+
).innerHTML = `Followers: ${data.followers}`;
32+
}
33+
};
34+
xmlRequest.send();
35+
</script>
36+
</html>

0 commit comments

Comments
 (0)