3

Working on the stream-adventure NodeJS tutorial, but am struggling with the duplexer redux exercise.

This is what I've tried to use, but it doesn't work:

var duplexer = require('duplexer2');
var through = require('through2').obj;

module.exports = function (counter) {
  var counts = {};
  var input = through(write, end);
  return duplexer(input, counter);

  var write = function (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  }

  var end = function (done) {
    counter.setCounts(counts);
    done();
  }
};

This is the proposed solution, which works:

var duplexer = require('duplexer2');
var through = require('through2').obj;

module.exports = function (counter) {
  var counts = {};
  var input = through(write, end);
  return duplexer(input, counter);

  function write (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  }

  function end (done) {
    counter.setCounts(counts);
    done();
  }
};

Can someone help me understand the difference between using the anonymous function saved into a variable versus just naming a function?

1
  • The function gets hoisted to the top of the scope (hence before the return), the variable assignment stays in place (hence after the return and not evaluated).
    – Sirko
    Mar 22, 2015 at 19:50

1 Answer 1

10

The difference is hoisting. When you use a function declaration statement (i.e. what's being used in the proposed solution), its definition is "hoisted" to the top of the scope (read: function) that contains it, and therefore even if you try to reference it before the code that defines it, it will be available.

You are using a variable assignment to define your functions. This means that write will not have a value until the var write = statement executes. You are trying to use it before that, when write still has the value undefined.

What this means is that you can get your code to work simply by moving the location where you define your functions:

module.exports = function (counter) {
  var counts = {};
  var write = function (row, _, next) {
    counts[row.country] = (counts[row.country] || 0) + 1;
    next();
  };
  var end = function (done) {
    counter.setCounts(counts);
    done();
  };
  var input = through(write, end);
  return duplexer(input, counter);
};

n.b. Do not confuse function declaration statements with named function expressions. named function expressions are unhoisted, just like anonymous functions:

var boom = getNumber();
var getNumber = function getNumber() { return 3; };
2
  • Agreed. All variables belong at the top of a function body.
    – t3dodson
    Mar 22, 2015 at 19:51
  • Not to necro the post, but typically most minifiers and even JS engines now days will automatically move all variables to the top of the function as an optimization step, though I typically still place all of mine at the top. Sep 10, 2015 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.