timoxley / functional-javascript-workshop

A functional javascript workshop. No libraries required (i.e. no underscore), just ES5.
2.06k stars 441 forks source link

Partial Application - Syntax Question #159

Closed nprz closed 8 years ago

nprz commented 8 years ago

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

felixsanz commented 8 years ago

@nprz My turn :stuck_out_tongue_winking_eye:

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 8 years ago

Ahhh, I get it. Thanks, man.