main.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
// Rest operator 
// Take the extra arguments and pack them down into an array

function talk(greeting, farewell, names) {
  // names argument should be an array of strings
  names.forEach(function(name) {
    console.log(`${greeting} ${name}!`)
    console.log(`${farewell} ${name}!`)
  })
}
talk('Hello', 'Goodbye', ['Larry', 'Moe', 'Curly'])

// To improve API, ideally, we wouldn't have to call the names as an array.
// e.g. 'Hello', 'Goodbye', 'Larry', 'Moe', 'Curly'

function talk2(greeting, farewell) {
  var names = Array.prototype.slice.call(arguments, 2)
  names.forEach(function(name) {
    console.log(`${greeting} ${name}!`)
    console.log(`${farewell} ${name}!`)
  })
}
talk2('Hello', 'Goodbye', 'Larry', 'Moe', 'Curly')

function talk3(greeting, farewell, ...names) {
  console.log(names)
}
talk3('Hello', 'Goodbye', 'Larry', 'Moe', 'Curly')
// [ 'Larry', 'Moe', 'Curly' ]

function betterTalk(greeting, farewell, ...names) {
  names.forEach(function(name) {
    console.log(`${greeting} ${name}!`)
    console.log(`${farewell} ${name}!`)
  })
}
betterTalk('Hello', 'Goodbye', 'Larry', 'Moe', 'Curly')
betterTalk('Hello', 'Goodbye', 'Adam', 'Eve')


// Spread operator
// Does the opposite: take an array and spread out into individual
var arr = ['Hello', 'Goodbye']
betterTalk (...arr, 'Larry', 'Moe', 'Curly')
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie