admin管理员组

文章数量:1323323

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?

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?

Share edited Mar 22, 2015 at 20:28 zilla asked Mar 22, 2015 at 19:48 zillazilla 9521 gold badge14 silver badges22 bronze badges 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 Commented Mar 22, 2015 at 19:50
Add a ment  | 

1 Answer 1

Reset to default 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; };

本文标签: javascriptNodeJS function vs anonymous functionStack Overflow