timoxley/functional-javascript-workshop

Partial Application - Syntax Question

nprz opened this issue · 2 comments

nprz commented

So the official solution is as follows:

var slice = Array.prototype.slice

  function logger(namespace) {
    return function() {
      console.log.apply(console, [namespace].concat(slice.call(arguments)))
    }
  }

  module.exports = logger

However, I am confused by the line

[namespace].concat(slice.call(arguments)

namespace is a string being passed in, no? What's with the brackets surrounding it? Why are they necessary?

Thanks in advanced

@nprz My turn 😜

namespace is a string. But you know, [] <- this means empty array. So basically [namespace] is an array with 1 element: namespace.

Converting that into an array let you use array methods like [].concat(), which doesn't work on strings.

nprz commented

Ahhh, I get it. Thanks, man.