-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-advanced-functions.html
135 lines (108 loc) · 3.04 KB
/
12-advanced-functions.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Advanced Functions</title>
</head>
<body>
<button onclick="" class="js-button">Click</button>
<script>
const buttonElement = document.querySelector('.js-button');
const eventListener = () => {
connsole.log('click');
}
// does same thing as onclick but allows you to add multiple listeners
buttonElement.addEventListener('click', ()=>{
console.log('click');
});
buttonElement.removeEventListener('click', () => {})
buttonElement.addEventListener('click', ()=>{
console.log('click2');
});
/*
const nums = 2;
// anonymous function (function with no name)
const function1 = function() {
console.log('hello2');
}
console.log(function1);
console.log(typeof function1);
// functions are values in javascript
// so we can store values in objects
const object1 = {
num: 2,
fun: function() {
console.log('hello3');
}
};
object1.fun();
function display(param) {
console.log(param);
}
display(2);
// passing in a function inside a function
// the following regular function syntax allows hoisting which means that the function can be used anywhere before it is actually made.
function run(param) {
}
run(function() {
console.log('hello4');
});
*/
/*
// asynchronous Code: computer wont wait for the following line to finish to continue
setTimeout(function(){
console.log('timeout')
}, 3000);
console.log('next line');
// will keep running function every 3 seconds
setInterval(function(){
console.log('interval')
}, 3000);
*/
/*
[
'make dinner',
'wash dishes',
'watch youtube'
].forEach((value, index) => {
if (value === 'wash dishes'){
return;
}
console.log(index);
console.log(value);
});
const regularFunction = function(param, param2) {
console.log('Hello');
return 5;
}
const arrowFunction = (param, param2) => {
console.log('hello');
return 5;
};
arrowFunction();
const oneParam = (param) => {
console.log(param +1);
};
oneParam(2);
const oneLine = () => 2+3;
console.log(oneLine());
*/
// in filter if the return value is true then the value is added to new array otherwise it isn't.
[1,-3,5].filter((value, index) => {
/*
if (value >= 0){
return true;
}
else{
return false;s
}
*/
return value >= 0;
})
// map will transform an array into another array based on return values
[1,1,3].map((value, index) => {
return value *2;
});
// closure= if a function has access to a value it will always have access to that function. so even if an index value is deleted after a loop the function will still have access to the index.
</script>
</body>
</html>