0

I just started last week learning JavaScript and Node.js. Before that I developed with Java WebObjects and VB.NET. I just want to learn it for my self.

My brain is hurting after this week because of closures and other JavaScript stuff.

And now the question. To create a simple Node server I always found some code like this.

var http = require("http");

http.createServer(function(request,response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(3000);

Is there any difference if I would write the code like this?

var http = require("http");


var serverCallback = function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write("Hello World");
    response.end();
}

var server = http.createServer(serverCallback);
server.listen(3000);

For me this is more readable. But I'm not really sure that its exact the same.

3
  • You meant "closures"? Commented Dec 14, 2013 at 12:00
  • 1
    Why should you do var serverCallback = function( and why not just function serverCallback(? ;) Commented Dec 14, 2013 at 12:15
  • there are usually 100s of callback in an web app, they are almost no shared and I never separate a callback if it is not shared. Commented Dec 14, 2013 at 14:30

2 Answers 2

1

There is no difference in functionality. Use whatever style you like.

Sign up to request clarification or add additional context in comments.

Comments

0

The only difference in this case how the variables are assigned, found this yesterday in HN https://news.ycombinator.com/item?id=7672131

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.