Repository navigation
Expand file tree
/
Copy path12-advanced-functions.html
More file actions
173 lines (137 loc) · 3.59 KB
/
Copy path12-advanced-functions.html
File metadata and controls
173 lines (137 loc) · 3.59 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
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>Advanced Functions</title>
<style>
</style>
</head>
<body>
<button onclick="" class="js-button"> Click</button>
<script>
/*
// .addEventListener and .removeEventListener:
const buttonElement = document.querySelector('.js-button');
const eventListener = () => {
console.log('click');
};
buttonElement.addEventListener('click',
eventListener);
buttonElement.removeEventListener('click', eventListener);
buttonElement.addEventListener('click', () => {
console.log('click2');
});
*/
/*
function greeting() {
console.log('hello');
}
greeting();
// Functions are Values
// we can save a function inside a Variable!
const num = 2;
const function1 = function () {
console.log('hello2');
}; //This ; is important Since we saving a Function inside a Variable !!
console.log(function1);
console.log(typeof function1);
function1();
const object1 = {
num: 2,
fun: function() {
console.log('hello3');
}
};
object1.fun();
// we can pass a value into a function
function display(param) {
console.log(param);
}
display(2);
// passing a function into another function
function run(param) {
param();
}
run (function() {
console.log('hello4')
});
*/
/*
// Practical Examples
// setTimeout
setTimeout(function(){
console.log('timeout');
console.log('timeout2');
}, 3000);
// Asynchronus Code and Synchronus Code
console.log('next line');
// setInterval
setInterval(function() {
console.log('interval');
}, 3000);
console.log('next line 2')
*/
/*
// another way to Loop through an array:
// .forEach() method:
[
'make dinner',
'wash dishes',
'watch youtube'
].forEach((value, index) => {
if (value === 'wash dishes') {
return; // does same thing as 'continue'.
}
console.log(index);
console.log(value);
});
// PART- 2:-
const regularFunction = function (param, param2) {
console.log('helllo');
return 5;
}
// Arrow Functions:
const arrowFunction = (param, param2) => {
console.log('hello');
return 5;
};
arrowFunction();
// we can use few shortcuts in arrow functions:
//oneParam:
const oneParam = param => {
console.log(param + 1);
};
oneParam(2);
oneParam(5);
//oneLine (return)
const oneLine = () => 2 + 3;
console.log(oneLine());
// when saving a function inside an Object, it is recommended to use the Shorthand Method syntax as it's more clear and easy to read than an Arrow function.
const object2 = {
method: () => {
},
method() {
// Shorthand Method
}
};
*/
// 2 more array methods:-
// i) .filter():
console.log([1, -3, 5].filter((value, index) => {
/*
if (value >= 0) {
return true;
} else {
return false;
}
*/
return value >= 0;
}));
// ii) .map():
console.log([1, 1, 3].map((value, index) => {
return value * 2;
}));
// some shortcuts of arrow functions:
console.log([1, 1, 3].map(value => value * 2));
</script>
</body>
</html>