2

What's the difference between:

var NodestrapGenerator = module.exports = function NodestrapGenerator() {
  yeoman.generators.Base.apply(this, arguments);
  // more code here
};

and:

var NodestrapGenerator = module.exports = function() {
  yeoman.generators.Base.apply(this, arguments);
  // more code here
};

I'm trying to write my yeoman-generator's index.js in coffeescript, but apparently the second snippet isn't the same because it's not working! Thanks!

Raine Revere
  • 30,985
  • 5
  • 40
  • 52
  • There should be no difference. The important part is that `module.exports` is set. – Brad May 01 '13 at 17:06
  • some code not show here must sniff the code and expects to see arguments.callee.name to correlate with the publicly-named variable. – dandavis May 01 '13 at 17:11
  • The first is a named function expression, the second an anonymous function expression. Some where in the `// more code here`, the name must be referred to for it to matter. – generalhenry May 01 '13 at 17:13

1 Answers1

4
var NodestrapGenerator = module.exports = function NodestrapGenerator() { ... };

This is a named function called "NodestrapGenerator", assigned to variable NodestrapGenerator.

var NodestrapGenerator = module.exports = function() { ... };

This is an unnamed function aka. anonymous function, which gets assigned to variable NodestrapGenerator.

In the end, they're both referencing the same function, so no difference which way you write it.

See this for more explanation: var functionName = function() {} vs function functionName() {}

As to why it's not working, its probably because the code is looking for the named function NodestrapGenerator to verify that it's the function it should be using, not some random anonymous function.

FYI, function has a property name which will get set to "NodestrapGenerator" in named function (first example), but will be empty string "" in anonymous function (second example). (See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/name)

Community
  • 1
  • 1
Amy
  • 7,388
  • 2
  • 20
  • 31