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
function add_intro() {
  console.log(arguments)
}
add_intro(1, 2, 3)
// { '0': 1, '1': 2, '2': 3 }
// not an array, but is array-like, no access to array methods like map & reduce

function add() {
  var result = Array.prototype.reduce.call(arguments, function(accum, currentVal) {
    return accum + currentVal
  })
  return result
}
/*
  More info:
  Array.prototype.reduce()
  Function.prototype.call()
*/

console.log(add(1, 2, 3))

function betterAdd_intro(...values) {
  console.log(values)
}
betterAdd_intro(1, 2, 3)
// [ 1, 2, 3 ]
// a proper array with access to array methods

function betterAdd(...values) {
  var result = values.reduce(function(accum, currentVal) {
    return accum + currentVal
  })
  return result
}
 console.log(betterAdd(1, 2, 3))
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie