1

Closures are functions that inherit variables from their enclosing environment. So lets see this code:

binaryServer = BinaryServer({port: 9001});

binaryServer.on('connection', function(client) {
  console.log("new connection");

  client.on('stream', function(stream, meta) {
    stream.on('data', function(data){
    //actions
    });
    stream.on('end', function() {
    //actions
    });
  });
});

So, I am correct if I say: " function(data){//actions} is a closure because inherits the variables of enclosing environment (like the object client) "? Because of my experiences that's what happening.

carduh
  • 529
  • 1
  • 4
  • 13

2 Answers2

2

Yes, that would be correct to say.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

Yes, You are correct.

Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

How do JavaScript closures work?

Community
  • 1
  • 1
Scarecrow
  • 4,057
  • 3
  • 30
  • 56