-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdvanceFunction.html
More file actions
168 lines (139 loc) · 3.73 KB
/
AdvanceFunction.html
File metadata and controls
168 lines (139 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!-- in this lesson: -->
<!-- ------------------- -->
<!--
Lesson 12 : Advance Functions
1. Functions are values
2. setTimeout(), setInterval()
3. forEach()
4. Arrow functions, .addEvenListener()
5. filter(), map()
6. Closure
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="" class="js-button">Click</button>
<script>
greeting() //🟨-Hoisting Declaration before Initialization
function greeting() {
console.log("hello")
}
greeting()
const num = 2
//🟨-Functions are values ( we can save it in a variable )
const function1 = function () /*-🟨Anonymous function (without name)*/ {
console.log("hello2")
}
console.log(function1)
console.log(typeof function1)
function1()
//🟨-Can save a value in a object ✔
const object1 = {
num: 2,
fun: function () {
//🟨-Method ( Function saved inside an object )
console.log("hello3")
},
}
object1.fun()
//🟨-Passing a value into a function
function display(param) {
console.log(param)
}
display(2)
function run(param) {
param()
}
run(function () {
console.log("hello4")
})
//🟨-setTimeout()
//🟩-allows us to run a function in the future
setTimeout(function () {
console.log("timeout")
console.log("timeout2")
}, 3000)
//🟨-How long to wait before running this function (milliseconds)
//🟨-Asyncronous Code ( won't wait for a line to finish before going to the next line )
console.log("next line")
//🟨-setInterval
setInterval(function () {
console.log("interval")
}, 3000)
//🟩-Keep running the function every 3 seconds
console.log("next line2")
x = [("make dinner", "wash dishes", "watch youtube")].forEach((value, index) => {
if (value === "wash dishes") {
return
}
console.log(index)
console.log(value)
})
//🟨if we want to use break; then its better to use a regular for loop.
//🟨-Regular Functions
const regularFunction = function (param, param2) {
console.log("hello")
}
//🟨-Arrow Functions
const arrowFunction = (param, param2) => {
console.log("hello")
return 5
}
arrowFunction()
const oneParam = (param) => {
console.log(param + 1)
}
oneParam(2)
//🟨 Shortcut
const oneLine = () => 2 + 3
console.log(oneLine())
const object2 = {
method: () => {},
method() {},
}
//🟨-addEventListener
const buttonElement = document.querySelector(".js-button")
const eventListener = () => {
console.log("click")
}
buttonElement.addEventListener("click", eventListener)
buttonElement.addEventListener("click", () => {
console.log("click2")
})
//🟨-removeEventListener
buttonElement.removeEventListener("click", eventListener)
//🟨 .filter() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//🟩 [1, -3, 5] => [1, 5] Negative numbers removed
console.log(
[1, -3, 5].filter((value, index) => {
/*
if (value >= 0) {
return true
} else {
return false
}
*/
return value >= 0
})
)
//🟨 .map() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//🟩 .map() = transform an array into another array
console.log(
[1, 1, 3].map((value, index) => {
return value * 2
})
)
//🟨 Shortcuts
console.log([1, 1, 3].map((value) => value * 2))
//🟨 Closure
//🟩 if a function has access to a value
//🟩 it will always have access to that value
//🟩 value gets packaged together (enclosed) with function
</script>
</body>
</html>