Function () () in javascript

I'm not sure how to invoke / form this question, but can someone explain to me what this code does below?

var routes = require("./routes/routes.js")(app); 

I see second () with the passed application, what does it do? https://github.com/couchbaselabs/restful-angularjs-nodejs/blob/master/app.js

To my surprise, in the above code, are variable routes not used at all in app.js? what is the purpose. Am I completely confused, here (app) argument does anything magical here?

+5
source share
1 answer

Design

 foo()(); 

expects foo() return a function and call it immediately. This is equivalent to more readable:

 var func = foo(); func(); 

A similar design you'll often see is:

 (function() { // function definition })(args); 

This defines the function and calls it immediately. The main use is to emulate a block area for variables.

+6
source

Source: https://habr.com/ru/post/1237450/


All Articles