-
Notifications
You must be signed in to change notification settings - Fork 5
/
arguments.js
66 lines (40 loc) · 1006 Bytes
/
arguments.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Array-like object & argument
// const names = ['Minh', 'Hung', 'Truong'];
// for (let i = 0; i < names.length; i++) {
// console.log(names[i])
// }
/* Cái gì array-like object ko có giống như array
1/ Ko có method
*/
const obj = {
0: 'Minh',
1: 'Hung',
2: 'Truong',
length: 3
}
for (let i = 0; i < obj.length; i++) {
console.log(obj[i])
}
// convert array-like object to array
console.log('convert array-like object to array');
console.log('Cach 1: ', Array.from(obj));
console.log('Cach 2: ', Array.prototype.slice.call(obj));
// with es6
function foo(...args) {
console.log('with es6: ', args);
}
foo(1, 23, 66, 25);
function sum() {
// let result = 0;
// for(let i = 0; i < arguments.length; i++) {
// result += arguments[i];
// }
// return result;
const numbers = Array.from(arguments);
return numbers.reduce(function(sum, num) {
return sum + num;
}, 0)
}
console.log(sum(5, 23, 55));
console.log('pack' == true)
// output: false