-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.js
39 lines (31 loc) · 902 Bytes
/
callback.js
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
console.log("=== FOR OF e FOR IN ===");
function soma() {
console.log(arguments);
for (let i in arguments) { // IN = retorna o index do array
console.log(i, arguments[i]);
}
let total = 0;
for (let num of arguments) { // OF = retorna o valor de cada index do array
total += num;
}
return total;
}
console.log(soma(15, 13, 11, 7));
console.log("=== CALLBACK ===");
function sum() {
let total = 0;
for (let x of arguments) {
console.log(x, x.constructor === Number);
if (x.constructor === Number) {
total += x;
}
}
// console.log(arguments[arguments.length - 1]);
// return total;
if (arguments[arguments.length - 1] instanceof Function) {
arguments[arguments.length - 1](total);
}
}
console.log(sum(1, 2, 3, function (valor) {
console.log("Chamou callback", valor * 2);
}));