0

I won't claim that I'm terribly well versed in Node, or even Javascript, but I've seen several modules of the form

module.exports = foo;

function foo() {
  ...
}

Now, I could see this working perhaps in this case, but I'm really confused when that module returns a function that is excuted.

module.exports = bar();

function bar() {
  ...
}

What is this witchcraft?

shortstuffsushi
  • 2,271
  • 19
  • 33
  • possible duplicate of [What is the purpose of NodeJS module.exports and how do you use it?](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-nodejs-module-exports-and-how-do-you-use-it) – Evan Davis Oct 31 '13 at 02:31
  • @Mathletics No, I understand *what* it is, it's the order of operations that I didn't understand. I had looked at that question, but it didn't answer what I was looking for. – shortstuffsushi Oct 31 '13 at 02:44
  • @Mathletics I've edited the title to more accurately reflect my question, and differentiate it further from the proposed duplicate. Please consider removing your vote to close. – shortstuffsushi Oct 31 '13 at 04:44
  • Ah, got it. That's called _hoisting_. – Evan Davis Oct 31 '13 at 15:04
  • possible duplicate of [How are javascript variables "hoisted" in these examples from MDN](http://stackoverflow.com/questions/14806135/how-are-javascript-variables-hoisted-in-these-examples-from-mdn) – glomad Oct 31 '13 at 15:15
  • @ithcy No, it's not related to that question at all. I honestly think you're just trolling. – shortstuffsushi Oct 31 '13 at 15:55
  • I'm honestly *not* trolling and yes, it is exactly related to that question which explains variable hoisting in javascript just like the accepted answer to this question does. – glomad Oct 31 '13 at 15:56
  • They are related only in the sense that they're both about hoisting. The scenarios between the questions are completely different. – shortstuffsushi Oct 31 '13 at 16:01
  • You asked "why does this happen?" The answer is "because hoisting." It has nothing to do with the scenarios, it's explained by a fundamental behavior of JavaScript. – glomad Oct 31 '13 at 16:04
  • I'm done feeding the trolls. I got my answer, so go ahead and close the question. – shortstuffsushi Oct 31 '13 at 16:09
  • I'm not trolling just because you disagree with me. This is not a personal attack on you, this is just how duplicates on SO work. It takes 5 votes to close a question. If 4 other people agree with me then the question will be closed. If not, it won't. The end. – glomad Oct 31 '13 at 16:11
  • This is not a duplicate, though. It's a separate question. Fundamentally, they both ask questions that *relate* to hoisting. I was not *aware* of function hoisting at all, so my question is in a sense much broader, where the other question asks about a specific ordering or functionality of hoisting. They are not related. The end. – shortstuffsushi Oct 31 '13 at 16:13
  • I think they are, but I can see your side, so I'll retract my close vote. Again, it's nothing personal. //Edit: retracted but there are still 2 close votes... not mine though :) – glomad Oct 31 '13 at 16:19
  • I appreciate the retracted vote. I apologize if my response in return seemed like a personal attack, I'm just tired of every question I ask being immediately closed. I mean, even before you it was voted for close for an entirely separate, and more distinctly unrelated duplicate. – shortstuffsushi Oct 31 '13 at 16:30
  • No offense taken. I apologize if I was too hasty in my close vote. – glomad Oct 31 '13 at 16:39

1 Answers1

5

Functions are defined at parse time, assignments are assigned at runtime. See this article http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ for more.

In short, the compiler makes 2 passes. With the following code:

var a = x;
function x( ) { }

In the first pass, var a and function x are declared and available in a symbol table (or some other form depending on the interpreter) after which the compiler makes a second pass performing the assignment of function x as to var a. At this stage, at any point (but limited to the language rules), function x is known.

Polity
  • 14,734
  • 2
  • 40
  • 40
  • Thank you for the answer. I was going to ask for more clarification after I had seen your original answer, but the link you've provided was basically exactly what I was looking for. Thanks again. – shortstuffsushi Oct 31 '13 at 02:46
  • 4
    @shortstuffsushi This is often referred to as "function hoisting". Might be helpful if you're trying to google it. – Aaron Dufour Oct 31 '13 at 03:07