2

I'm fairly new to JavaScript (learning Node.JS) but I've been coding in OOP languages for quite some time. I feel like I understand most JS concepts but I'd love to learn more about this feature that has my mind boggled.

How does the argument binding work in this? How does console.log() get data here? Does this only work if there is only 1 default argument?

function processResponse (res) {
  res.on('data', console.log)
  res.on('error', console.error)
}

What is it called when the callback function doesn't need to be explicit about the argument it receives? Is it related to Function.prototype.apply()/call()/bind()? Just wanted to further my understanding.

EDIT: I do realize that functions references can be passed around. I was wondering how console.log receives its arguments here (what is the behind-the-scenes mechanism)

Cymk
  • 88
  • 1
  • 7
  • It's not called anything. Callback functions can't be passed arguments directly by your code. If they need to then they have to be wrapped in another function that doesn't take any and that wrapper function can then call the true callback function with whatever arguments it needs. – Scott Marcus Jun 15 '18 at 21:36
  • 1
    It is a function reference, similar to function pointers in C or method references in Java – Trash Can Jun 15 '18 at 21:36
  • Functions are values, like strings, numbers, .... . In fact, they are "callable" *objects*. `console.log` is just a property that has a function as a value. As such you can pass them around like any other value. – Felix Kling Jun 15 '18 at 21:41
  • 1
    I think your question might be a duplicate of or at least explained by [Create a custom callback in JavaScript](https://stackoverflow.com/q/2190850/218196) – Felix Kling Jun 15 '18 at 21:46

1 Answers1

3

Functions are first-class citizens in Javascript - you can pass them around just like you can pass around any other variable. In your code, it's not that console.log doesn't need any arguments, it's that you're passing the function itself, which then gets called with the needed arguments later.

Assuming that res.on calls its callback with only one argument, for simplicity:

res.on('data', console.log)

is exactly the same as

res.on('data', (arg) => console.log(arg))

A function that accepted no arguments, by contrast, would look something like this:

res.on('data', () => console.log())

If you're passing the function itself like in the original code, you can't tell just by looking at that line how many arguments the function actually accepts or how many arguments the callback is called with, though - might be none, might be many.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I understand that function references can be passed but I was wondering what the mechanism of how it is later called worked – Cymk Jun 15 '18 at 21:42
  • 1
    @Cymk: These functions are called like any other function. Imagine the implementation for `res.on` to be something like `function on(event, callback) { /* if "event" happens*/ callback(/*optionally pass some data */); }`. – Felix Kling Jun 15 '18 at 21:43
  • @FelixKling this comment perfectly answers my question but alas I can't mark a comment as an answer. If you could, please submit it as an answer and I'll mark it answered. – Cymk Jun 19 '18 at 09:23