-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionArgs.js
62 lines (54 loc) · 1.76 KB
/
functionArgs.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
59
60
61
62
// .load ./functionArgs.js //in command line
function average(x, y) {
return y === undefined? x : (x+y)/2
}
console.log(average(2))
function average2(x=0, y=x) { // default arg
return (x+y)/2
}
console.log(average2(3))
console.log(average2())
console.log(average2(null)) //0?
console.log(average2(1,2,3))
// capture extra args in `rest` parameter(rest - take seq, gather them in array)
function average3(...args) {
let sum = 0;
for(x of args) sum +=x;
return sum/Math.max(1, args.length);
}
console.log(average3(1,2,3,4))
// console.log(average3([1,2, 3])) // Error!
// use spread operator if arguments are already in an array or iterable(spread, take array, give sequence)
console.log(average3(...[1,2,3]))
//spread also works inside array literals
let numbers = [1,2,3,5]
const clone = [...numbers] // copy, change clone, numbers won't change
const chars = [...'Hello 🌐']
console.log(clone)
console.log(chars)
// simulating named arguments with destructuring
mkstring(numbers, {leftDelimiter:'{', rightDelimiter:'}'})
function mkstring(array, config={}) {
//look up properties and set defaults
if(config.separator===undefined) config.separator=','
console.log(config.leftDelimiter)
for(i of array) {
console.log(i, config.separator)
}
console.log(config.rightDelimiter)
}
// neat trick: use destructuring
function mkstring2(array,
{leftDelimiter = '[', rightDelimiter = ']', separator = ','}={}) {
let res = leftDelimiter;
for(const ele of array) {
if(res!==leftDelimiter) res+=separator;
res += ele;
}
res += rightDelimiter;
console.log(res);
// return res;
}
mkstring2(numbers);
mkstring2(numbers, {separator:" and "});
mkstring2(numbers, {leftDelimiter:'{', rightDelimiter:'}'})