0 votes
in JavaScript by
Explain Closures in JavaScript.

1 Answer

0 votes
by

Closures is an ability of a function to remember the variables and functions that are declared in its outer scope.

var Person = function(pName){

  var name = pName;

  this.getName = function(){

    return name;

  }

}

var person = new Person("Neelesh");

console.log(person.getName());

Let’s understand closures by example:

function randomFunc(){

  var obj1 = {name:"Vivian", age:45};

  return function(){

    console.log(obj1.name + " is "+ "awesome"); // Has access to obj1 even when the randomFunc function is executed

  }

}

var initialiseClosure = randomFunc(); // Returns a function

initialiseClosure(); 

Let’s understand the code above,

The function randomFunc() gets executed and returns a function when we assign it to a variable:

var initialiseClosure = randomFunc();

The returned function is then executed when we invoke initialiseClosure:

 initialiseClosure(); 

The line of code above outputs “Vivian is awesome” and this is possible because of closure.

When the function randomFunc() runs, it sees that the returning function is using the variable obj1 inside it:

console.log(obj1.name + " is "+ "awesome");

Therefore randomFunc(), instead of destroying the value of obj1 after execution, saves the value in the memory for further reference. This is the reason why the returning function is able to use the variable declared in the outer scope even after the function is already executed.

This ability of a function to store a variable for further reference even after it is executed, is called Closure.

Related questions

0 votes
asked Aug 30, 2020 in Python by sharadyadav1986
0 votes
asked Oct 28, 2023 in JavaScript by DavidAnderson
...