function outerFn (myArg) {At the same time, such variables continue to exist and remain accessible by an internal function even after the external function in which they are defined has been executed.
var myVar;
function innerFn () {
// has access to myVar and myArg
}
}
function createCounter () {In this example, the function returned by createCounter uses the variable numberOfCalls, which stores the desired value between its calls (instead of immediately ending its existence with the return of createCounter).
var numberOfCalls = 0;
return function () {
return ++ numberOfCalls;
}
}
var fn = createCounter ();
fn (); //one
fn (); // 2
fn (); // 3
var fn = ( function () {Such a construction allowed us to bind data to a function that persists between its calls — this is one of the uses of closures. In other words, with the help of them we can create functions that have their changing state.
var numberOfCalls = 0;
return function () {
return ++ numberOfCalls;
}
}) ();
var createHelloFunction = function (name) {Due to the closure, the returned function “remembers” the parameters passed to the function creating what we need for this kind of things.
return function () {
alert ( 'Hello,' + name);
}
}
var sayHelloHabrahabr = createHelloFunction ( 'Habrahabr' );
sayHelloHabrahabr (); // alerts “Hello, Habrahabr”
Function.prototype.bind = function (context) {In this example, with the help of closures, a function bossed by the bind remembers the initial function and the context assigned to it.
var fn = this ;
return function () {
return fn.apply (context, arguments);
};
}
var HelloPage = {
name: 'Habrahabr' ,
init: function () {
alert ( 'Hello,' + this .name);
}
}
//window.onload = HelloPage.init; // alert would be undefined, tk this would point to a window
window.onload = HelloPage.init.bind (HelloPage); // now everything works
( function () {Obviously, inside the closure, we have access to all external data, but it also has its own. Due to this, we can surround parts of the code with a similar construction in order to close local variables that have fallen inside from access outside. (One of the examples of its use you can see in the source code of the jQuery library, which surrounds all its code with a closure, so as not to output the variables necessary only for it).
...
}) ();
( function () {
// the higher this will be saved
}). call ( this );
var MyModule = {With the help of a closure, we can make methods and properties that are not used outside the object private (i.e., accessible only to it):
name: 'Habrahabr' ,
sayPreved: function (name) {
alert ( 'PREVED' + name.toUpperCase ())
},
sayPrevedToHabrahabr: function () {
this .sayPreved ( this .name);
}
}
MyModule.sayPrevedToHabrahabr ();
var MyModule = ( function () {
var name = 'Habrahabr' ;
function sayPreved () {
alert ( 'PREVED' + name.toUpperCase ());
}
return {
sayPrevedToHabrahabr: function () {
sayPreved (name);
}
}
}) ();
MyModule.sayPrevedToHabrahabr (); // alerts "PREVED Habrahabr"
for ( var i = 0; i <links.length; i ++) {In fact, it turns out that when you click on any link, the same number is displayed - the value of links.length. Why it happens? In connection with the closure, the declared auxiliary variable i continues to exist, and at that moment when we click on the link. Since by that time the cycle has already passed, i remains equal to the number of links - this is the value we see with clicks.
links [i] .onclick = function () {
alert (i);
}
}
for ( var i = 0; i <links.length; i ++) {Here, using another closure, we “shade” the variable i, creating its copy in its local scope at each step of the cycle. Thanks to this, everything now works as intended.
( function (i) {
links [i] .onclick = function () {
alert (i);
}
}) (i);
}
Source: https://habr.com/ru/post/38642/
All Articles