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
1 Answer
Reset to default 10The 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
版权声明:本文标题:javascript - NodeJS: function vs. anonymous function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142681a2422653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论