0
  • i am new to js
  • I thought closure as inner function as access to variables in outer function.
  • but in the below code if separate function has access to another separate function will it form closure
//separate function
function auth(name) {  
  return function (req, res, next) {
    if (req.isAuthenticated() && name && req.user.name === name) next();
    else if (req.isAuthenticated() && !name) next();
    else res.send(401);
  };
}

//separate function but uses auth

app.get('/example/a', auth(), function (req, res) {  
  res.send('Hello from A!');
});

 //separate function but uses auth

app.get('/example/b', auth('Francis'), function (req, res) {  
  res.send('Hello from B!');
});
  • 3
    *"I thought closure as inner function as access to variables in outer function."* Yes, basically. *"if separate function has access to another separate function will it form closure"* I don't understand what you mean by that. Which functions in your example are you referring to? – Felix Kling Apr 19 '17 at 18:20
  • I'm afraid the edit doesn't make things much clearer. – T.J. Crowder Apr 19 '17 at 18:26
  • @FelixKling auth is separate function, can you tell me whether in my code are we using closures concept –  Apr 19 '17 at 18:26
  • Yes; the anonymous function in `auth` uses `name` from its containing context. – Dave Newton Apr 19 '17 at 18:27
  • In theory, *every* function in JavaScript is a closure. In your particular case, the function *returned* by `auth` accesses `name` which is defined outside of it (the inner function) (`name` a parameter of `auth`), so it's a closure for all intents and purposes. – Felix Kling Apr 19 '17 at 18:28
  • @FelixKling can you give comments in my code its confusing –  Apr 19 '17 at 18:33
  • I don't know what your problem is. You said *"I thought closure as inner function as access to variables in outer function."* which is basically correct and is exactly what you are doing in your example. Given that you understand that (at least that's how interpret this statement), what else are you confused about? – Felix Kling Apr 19 '17 at 18:38
  • @FelixKling I didn't understand this line----> In your particular case, the function returned by auth accesses name which is defined outside of it (the inner function) (name a parameter of auth), so it's a closure for all intents and purposes. –  Apr 19 '17 at 18:41
  • `auth` returns a function. That inner function accesses `name` which is a parameter of `auth` and therefore defined outside of the inner function. I'm just describing your code and confirming your statement *"I thought closure as inner function as access to variables in outer function."*. `name` is "a variable in outer function". The function returned by `auth` is "inner function". – Felix Kling Apr 19 '17 at 18:44
  • @FelixKling I though name is a parameter and not a variable so it does not form closure –  Apr 19 '17 at 19:26
  • Parameters and variables are both *bindings* in the execution context so they are basically the same. The only difference between them is where the value comes from. – Felix Kling Apr 19 '17 at 19:28

1 Answers1

1

but in the below code if separate function has access to another separate function will it form closure

The function auth returns is a closure over the context of the call to auth where it's created, which is why it has access to the name parameter even though auth has returned by the time the function is called. It is not a closure over anything else relevant; req, res, and next are parameters it receives, not something it closes over.

So the overall code in the question creates two closures over two separate contexts (one each for each of the two calls to auth).

That code is fine if the goal is to create a function that uses the name you're passing auth later when it's called. It's a classic use of closures.


Related:

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • *Made this a Community Wiki because I suspect in the end, when we're sure we understand the question, it'll probably be a duplicate of [How do JavaScript closures work](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1)...* – T.J. Crowder Apr 19 '17 at 18:30
  • @all can you give comments in my code its confusing –  Apr 19 '17 at 18:33
  • @texirv: Study the links provided above. I don't think comments in your code would help any. *"The function `auth` returns"* is unambiguous...? – T.J. Crowder Apr 19 '17 at 20:44