2
Reply

What are Javascript closures? Explain its Uses?

Atul Jain

Atul Jain

12y
1.6k
0
Reply

    A closure is a pairing of a function along with its referencing environment such that identifiers within the function may refer to variables declared within the referencing environment.It's an inner function that has access to the outer (enclosing) function's variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables.function showName (firstName, lastName) {? ?var nameIntro = "Your name is ";// this inner function has access to the outer function's variables, including the parameter? ?function makeFullName () {? ?return nameIntro + firstName + " " + lastName;? } ? ?return makeFullName ();? }? ? showName ("Michael", "Jackson"); Closures can be used to accomplish many things. They are very useful for things like configuring callback functions with parameters.Closures are useful when used in conjunction with the setTimeout() and setInterval() functions.

    A closure is an inner function that has access to the outer (enclosing) function's variables as well as its own.You can use closures to implement encapsulation something like this,a = (function () {var privatefunction = function () {alert('hello');}return {publicfunction : function () {privatefunction();}} })();