Skip to main content
added 1 characters in body
Source Link
BenMorel
  • 37.1k
  • 53
  • 208
  • 339

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in JavaScript, every function is a closure. It always has an access to variables defined in the surrounding scope.

Since scope-defining construction in JavaScript is a function, not a code block like in many other languages, what we usually mean by closure in JavaScript is a fuctionfunction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
    // Create a hidden object, which will hold the data
    // it's inaccessible from the outside.
    var data = {};

    // Make a function, which will provide some access to the data.
    return function(key, val) {
        if (val === undefined) { return data[key] } // Get
        else { return data[key] = val } // Set
    }
    // We are calling the anonymous surrounding function,
    // returning the above inner function, which is a closure.
})();

db('x')    // -> undefined
db('x', 1) // Set x to 1
db('x')    // -> 1
// It's impossible to access the data object itself.
// We are able to get or set individual it.

ems

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Another usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in JavaScript, every function is a closure. It always has an access to variables defined in the surrounding scope.

Since scope-defining construction in JavaScript is a function, not a code block like in many other languages, what we usually mean by closure in JavaScript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
    // Create a hidden object, which will hold the data
    // it's inaccessible from the outside.
    var data = {};

    // Make a function, which will provide some access to the data.
    return function(key, val) {
        if (val === undefined) { return data[key] } // Get
        else { return data[key] = val } // Set
    }
    // We are calling the anonymous surrounding function,
    // returning the above inner function, which is a closure.
})();

db('x')    // -> undefined
db('x', 1) // Set x to 1
db('x')    // -> 1
// It's impossible to access the data object itself.
// We are able to get or set individual it.

ems

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Another usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in JavaScript, every function is a closure. It always has an access to variables defined in the surrounding scope.

Since scope-defining construction in JavaScript is a function, not a code block like in many other languages, what we usually mean by closure in JavaScript is a function working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
    // Create a hidden object, which will hold the data
    // it's inaccessible from the outside.
    var data = {};

    // Make a function, which will provide some access to the data.
    return function(key, val) {
        if (val === undefined) { return data[key] } // Get
        else { return data[key] = val } // Set
    }
    // We are calling the anonymous surrounding function,
    // returning the above inner function, which is a closure.
})();

db('x')    // -> undefined
db('x', 1) // Set x to 1
db('x')    // -> 1
// It's impossible to access the data object itself.
// We are able to get or set individual it.

ems

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Another usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Post Made Community Wiki by Max Tkachenko
Copy edited.
Source Link
Peter Mortensen
  • 31.2k
  • 22
  • 111
  • 134

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in JavascriptJavaScript, every function is a closure. It always has an access to variables defined in the surrounding scope.

Since scope-defining construction in JavascriptJavaScript is a function, not a code block like in many other languages, what we usually mean by closure in JavascriptJavaScript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
    // createCreate a hidden object, which will hold the data
    // it's inaccessible from the outside.
    var data = {}; 

    // makeMake a function, which will provide some access to the data.
    return function(key, val) {
        if (val === undefined) { return data[key] } // getGet
        else { return data[key] = val } // setSet
    }
    // weWe are calling the anonymous surrounding function,
    // returning the above inner function, which is a closure.
})();

db('x')    // -> undefined
db('x', 1) // setSet x to 1
db('x')    // -> 1
// it'sIt's impossible to access the data object itself.
// weWe are able to get or set individual itemsit.

ems

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. OtherAnother usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in Javascript, every function is a closure. It always has an access to variables defined in the surrounding scope

Since scope-defining construction in Javascript is a function, not a code block like in many other languages, what we usually mean by closure in Javascript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
  // create a hidden object, which will hold the data
  // it's inaccessible from outside
  var data = {};
  // make a function, which will provide some access to the data
  return function(key, val) {
    if (val === undefined) { return data[key] } // get
    else { return data[key] = val } // set
  }
  // we are calling the anonymous surrounding function,
  // returning the above inner function, which is a closure
})();

db('x')    // -> undefined
db('x', 1) // set x to 1
db('x')    // -> 1
// it's impossible to access the data object itself
// we are able to get or set individual items

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Other usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in JavaScript, every function is a closure. It always has an access to variables defined in the surrounding scope.

Since scope-defining construction in JavaScript is a function, not a code block like in many other languages, what we usually mean by closure in JavaScript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
    // Create a hidden object, which will hold the data
    // it's inaccessible from the outside.
    var data = {}; 

    // Make a function, which will provide some access to the data.
    return function(key, val) {
        if (val === undefined) { return data[key] } // Get
        else { return data[key] = val } // Set
    }
    // We are calling the anonymous surrounding function,
    // returning the above inner function, which is a closure.
})();

db('x')    // -> undefined
db('x', 1) // Set x to 1
db('x')    // -> 1
// It's impossible to access the data object itself.
// We are able to get or set individual it.

ems

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Another usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

slight enhancement +typo fix in code
Source Link
mykhal
  • 20.1k
  • 11
  • 85
  • 84

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in Javascript, every function is a closure. It always has an access to variables defined in the surrounding scope

Since scope-defining construction in Javascript is a function, not a code block like in many other languages, what we usually mean by closure in Javascript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
  // create a hidden object, which will hold the data
  // it's inaccessible from outside
  var data = {};
  // make a function, which will haveprovide accessome access to the data
  return function(key, val) {
    if (val === undefined) { return data[key] } // get
    else { return data[key] = val } // set
  }
  // we are calling the anonymous surrounding function,
  // returning the above inner function, which is a closure
})();

db('x')    // -> undefined
db('x', 1) // set x to 1
db('x')    // -> 1
// it's impossible to access the data object itself
// we are able to get or set individual items

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Other usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in Javascript, every function is a closure. It always has an access to variables defined in the surrounding scope

Since scope-defining construction in Javascript is a function, not a code block like in many other languages, what we usually mean by closure in Javascript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
  // create a hidden object, which will hold the data
  // it's inaccessible from outside
  var data = {};
  // make a function, which will have acces to the data
  return function(key, val) {
    if (val === undefined) { return data[key] } // get
    else { return data[key] = val } // set
  }
  // we are calling the anonymous surrounding function,
  // returning the above inner function, which is a closure
})();

db('x')    // -> undefined
db('x', 1) // set x to 1
db('x')    // -> 1
// it's impossible to access the data object itself
// we are able to get or set individual items

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Other usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

Wikipedia on closures:

In computer science, a closure is a function together with a referencing environment for the nonlocal names (free variables) of that function.

Technically, in Javascript, every function is a closure. It always has an access to variables defined in the surrounding scope

Since scope-defining construction in Javascript is a function, not a code block like in many other languages, what we usually mean by closure in Javascript is a fuction working with nonlocal variables defined in already executed surrounding function.

Closures are often used for creating functions with some hidden private data (but it's not always the case).

var db = (function() {
  // create a hidden object, which will hold the data
  // it's inaccessible from outside
  var data = {};
  // make a function, which will provide some access to the data
  return function(key, val) {
    if (val === undefined) { return data[key] } // get
    else { return data[key] = val } // set
  }
  // we are calling the anonymous surrounding function,
  // returning the above inner function, which is a closure
})();

db('x')    // -> undefined
db('x', 1) // set x to 1
db('x')    // -> 1
// it's impossible to access the data object itself
// we are able to get or set individual items

The example above is using an anonymous function, which was executed once. But it does not have to be. It can be named (e.g. mkdb) and executed later, generating a database function each time it is invoked. Every generated function will have its own hidden database object. Other usage example of closures is when we don't return a function, but an object containing multiple functions for different purposes, each of those function having access to the same data.

code example comments
Source Link
mykhal
  • 20.1k
  • 11
  • 85
  • 84
Loading
remove the first dummy example
Source Link
mykhal
  • 20.1k
  • 11
  • 85
  • 84
Loading
Source Link
mykhal
  • 20.1k
  • 11
  • 85
  • 84
Loading